Format Dates in Text Elements with Code

Formatting dates within text elements using code allows you to display dates dynamically and tailor them to different locales.

Use JavaScript's Date class' toLocaleDateString method to format dates on your site.

Format dates on your site

  1. To render a date in the site visitor's locale settings on their computer, call toLocaleDateString() on your date object without specifying any parameters. For example:

    Copy
  2. Add parameters to customize the date's locale. For example:

    Copy

Format dates from a dataset on your site

If you're displaying dates retrieved from a dataset, you can format all dates retrieved from your dataset in the same way using the dataset element's onReady() and getCurrentItem() with toLocaleDateString(). For example:

Copy

Note: You can also format dates from a dataset in the editor without code.

See also

Did this help?