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.
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:
Add parameters to customize the date's locale. For example:
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:
Note: You can also format dates from a dataset in the editor without code.