Namespace: Dates:format

Dates:format

Source:

Methods

format(date, definitionopt, nullable, localeopt, typeopt, nullable, optionsopt, nullable) → {String}

Returns a formatted string, describing the date in a verbose, non-technical way.

Under the hood, this uses Intl.DateTimeFormat, which is widely supported and conveniently to use for most widely used locales.

"definition" may be a format shortcut for "dateStyle" (and "timeStyle" if type is "datetime") or a format string, for a custom format, using these tokens:

YY 18 two-digit year; YYYY 2018 four-digit year; M 1-12 the month, beginning at 1; MM 01-12 the month, 2-digits; D 1-31 the day of the month; DD 01-31 the day of the month, 2-digits; H 0-23 the hour; HH 00-23 the hour, 2-digits; h 1-12 the hour, 12-hour clock; hh 01-12 the hour, 12-hour clock, 2-digits; m 0-59 the minute; mm 00-59 the minute, 2-digits; s 0-59 the second; ss 00-59 the second, 2-digits; SSS 000-999 the millisecond, 3-digits; Z +05:00 the offset from UTC, ±HH:mm; ZZ +0500 the offset from UTC, ±HHmm; A AM PM; a am pm;

Using these, you could create your own ISO string like this: "YYYY-MM-DDTHH:mm:ss.SSSZ"

If you use "full", "long", "medium" or "short" instead, you'll use the DateTimeFormatters built-in, preset format styles for localized dates, based on the given locale(s).

Parameters:
Name Type Attributes Default Description
date Date

the date to format

definition String <optional>
<nullable>
'long'

either a preset style to quickly define a format style, by setting shortcuts for dateStyle and timeStyle (if type is "datetime"), set to "none" or nullish value to skip quick format; alternatively, define this as a format string to use a custom format

locale String | Array.<String> <optional>
'en-US'

locale to use for date format and text generation, use array to define fallback; always falls back to en-US if nothing else works

type String <optional>
<nullable>
'datetime'

set to 'datetime', 'date' or 'time' to define which parts should be rendered

options Object <optional>
<nullable>
null

options to pass to the Intl.DateTimeFormat constructor, is applied last, so should override anything predefined, if key is reset

Source:
See:
Returns:
- the formatted date/time string
Type
String
Example
format(new Date(), 'de-DE', 'long', 'datetime', {timeZone : 'UTC'})
=> '12. Dezember 2023 um 02:00:00 UTC'
format(new Date(), 'YYYY-MM-DDTHH:mm:ss.SSSZ')
=> '2023-12-12T02:00:00'