Skip to main content

format

function format(format_string: string, locale?: string): string

Constructs a formatted date string from format_string. To localize the output, pass a locale as the second argument.

caution

Formatting localized strings may be slower than other libraries, as Chronosis uses Intl.DateTimeFormat for all localizations.

note

If you are looking for simplified stringification (for use in debugging, etc), see toString.

const date = new Chronosis('2023-09-12')
// Returns 'Today is September 12, 2023'.
const formatted = date.format('[Today is] MMMM D, YYYY')

The table below uses the current date, rendered via Chronosis!

InputOutputDescription
[escapeYY]escapeYYEscaped characters
YY23Two-digit year
YYYY2023Four-digit year
M9Month, starting at 1
MM09Two-digit month
MMMSepThree-character month name
MMMMSeptemberFull month name
D15Day of the month
DD15Two-digit day of the month
d5Weekday
ddFTwo-character weekday name
dddFriThree-character weekday name
ddddFridayFull weekday name
H3Hour
HH03Two-digit hour
h312-hour clock hour
hh03Two-digit 12-hour clock hour
m11Minute
mm11Two-digit minute
s45Second
ss45Two-digit second
SSS538Three-digit millisecond
Z+00:00UTC offset
aamLowercase meridiem
AAMUppercase meridiem

If you can't find a way to display the text you want, either file an feature request or write your own code using the date returned by toDate.