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
!
- English
- Español
- 日本語
Input | Output | Description |
---|---|---|
[escapeYY] | escapeYY | Escaped characters |
YY | 23 | Two-digit year |
YYYY | 2023 | Four-digit year |
M | 9 | Month, starting at 1 |
MM | 09 | Two-digit month |
MMM | Sep | Three-character month name |
MMMM | September | Full month name |
D | 15 | Day of the month |
DD | 15 | Two-digit day of the month |
d | 5 | Weekday |
dd | F | Two-character weekday name |
ddd | Fri | Three-character weekday name |
dddd | Friday | Full weekday name |
H | 3 | Hour |
HH | 03 | Two-digit hour |
h | 3 | 12-hour clock hour |
hh | 03 | Two-digit 12-hour clock hour |
m | 11 | Minute |
mm | 11 | Two-digit minute |
s | 45 | Second |
ss | 45 | Two-digit second |
SSS | 538 | Three-digit millisecond |
Z | +00:00 | UTC offset |
a | am | Lowercase meridiem |
A | AM | Uppercase meridiem |
Input | Output | Description |
---|---|---|
[escapeYY] | escapeYY | Escaped characters |
YY | 23 | Two-digit year |
YYYY | 2023 | Four-digit year |
M | 9 | Month, starting at 1 |
MM | 09 | Two-digit month |
MMM | sept | Three-character month name |
MMMM | septiembre | Full month name |
D | 15 | Day of the month |
DD | 15 | Two-digit day of the month |
d | 5 | Weekday |
dd | V | Two-character weekday name |
ddd | vie | Three-character weekday name |
dddd | viernes | Full weekday name |
H | 3 | Hour |
HH | 03 | Two-digit hour |
h | 3 | 12-hour clock hour |
hh | 03 | Two-digit 12-hour clock hour |
m | 11 | Minute |
mm | 11 | Two-digit minute |
s | 45 | Second |
ss | 45 | Two-digit second |
SSS | 546 | Three-digit millisecond |
Z | +00:00 | UTC offset |
a | am | Lowercase meridiem |
A | AM | Uppercase meridiem |
Input | Output | Description |
---|---|---|
[escapeYY] | escapeYY | Escaped characters |
YY | 23 | Two-digit year |
YYYY | 2023 | Four-digit year |
M | 9 | Month, starting at 1 |
MM | 09 | Two-digit month |
MMM | 9月 | Three-character month name |
MMMM | 9月 | Full month name |
D | 15 | Day of the month |
DD | 15 | Two-digit day of the month |
d | 5 | Weekday |
dd | 金 | Two-character weekday name |
ddd | 金 | Three-character weekday name |
dddd | 金曜日 | Full weekday name |
H | 3 | Hour |
HH | 03 | Two-digit hour |
h | 3 | 12-hour clock hour |
hh | 03 | Two-digit 12-hour clock hour |
m | 11 | Minute |
mm | 11 | Two-digit minute |
s | 45 | Second |
ss | 45 | Two-digit second |
SSS | 549 | Three-digit millisecond |
Z | +00:00 | UTC offset |
a | am | Lowercase meridiem |
A | AM | Uppercase 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.