Back to Luxon

Formatting

docs/formatting.md

3.7.222.9 KB
Original Source

Formatting

This section covers creating strings to represent a DateTime. There are three types of formatting capabilities:

  1. Technical formats like ISO 8601 and RFC 2822
  2. Internationalizable human-readable formats
  3. Token-based formatting

Technical formats (strings for computers)

ISO 8601

ISO 8601 is the most widely used set of string formats for dates and times. Luxon can parse a wide range of them, but provides direct support for formatting only a few of them:

js
dt.toISO(); //=> '2017-04-20T11:32:00.000-04:00'
dt.toISODate(); //=> '2017-04-20'
dt.toISOWeekDate(); //=> '2017-W17-7'
dt.toISOTime(); //=> '11:32:00.000-04:00'

Generally, you'll want the first one. Use it by default when building or interacting with APIs, communicating times over a wire, etc.

HTTP and RFC 2822

There are a number of legacy standard date and time formats out there, and Luxon supports some of them. You shouldn't use them unless you have a specific reason to.

js
dt.toRFC2822(); //=> 'Thu, 20 Apr 2017 11:32:00 -0400'
dt.toHTTP(); //=> 'Thu, 20 Apr 2017 03:32:00 GMT'

Unix timestamps

DateTime objects can also be converted to numerical Unix timestamps:

js
dt.toMillis(); //=> 1492702320000
dt.toSeconds(); //=> 1492702320.000
dt.toUnixInteger(); // => 1492702320
dt.valueOf(); //=> 1492702320000, same as .toMillis()

toLocaleString (strings for humans)

The basics

Modern browsers (and other JS environments) provide support for human-readable, internationalized strings. Luxon provides convenient support for them, and you should use them anytime you want to display a time to a user. Use toLocaleString to do it:

js
dt.toLocaleString(); //=> '4/20/2017'
dt.toLocaleString(DateTime.DATETIME_FULL); //=> 'April 20, 2017 at 11:32 AM EDT'
dt.setLocale('fr').toLocaleString(DateTime.DATETIME_FULL); //=> '20 avril 2017 à 11:32 UTC−4'

Intl.DateTimeFormat

In the example above, DateTime.DATETIME_FULL is one of several convenience formats provided by Luxon. But the arguments are really any object of options that can be provided to Intl.DateTimeFormat. For example:

js
dt.toLocaleString({ month: 'long', day: 'numeric' }); //=> 'April 20'

And that's all the preset is:

DateTime.DATETIME_FULL;  //=> {
                         //     year: 'numeric',
                         //     month: 'long',
                         //     day: 'numeric',
                         //     hour: 'numeric',
                         //     minute: '2-digit',
                         //     timeZoneName: 'short'
                         //   }

This also means you can modify the presets as you choose:

js
dt.toLocaleString(DateTime.DATE_SHORT); //=>  '4/20/2017'
var newFormat = {...DateTime.DATE_SHORT, weekday: 'long' };
dt.toLocaleString(newFormat); //=>  'Thursday, 4/20/2017'

Presets

Here's the full set of provided presets using the October 14, 1983 at 13:30:23 as an example.

NameDescriptionExample in en_USExample in fr
DATE_SHORTshort date10/14/198314/10/1983
DATE_MEDabbreviated dateOct 14, 198314 oct. 1983
DATE_MED_WITH_WEEKDAYabbreviated date with abbreviated weekdayFri, Oct 14, 1983ven. 14 oct. 1983
DATE_FULLfull dateOctober 14, 198314 octobre 1983
DATE_HUGEfull date with weekdayFriday, October 14, 1983vendredi 14 octobre 1983
TIME_SIMPLEtime1:30 PM13:30
TIME_WITH_SECONDStime with seconds1:30:23 PM13:30:23
TIME_WITH_SHORT_OFFSETtime with seconds and abbreviated named offset1:30:23 PM EDT13:30:23 UTC−4
TIME_WITH_LONG_OFFSETtime with seconds and full named offset1:30:23 PM Eastern Daylight Time13:30:23 heure d’été de l’Est
TIME_24_SIMPLE24-hour time13:3013:30
TIME_24_WITH_SECONDS24-hour time with seconds13:30:2313:30:23
TIME_24_WITH_SHORT_OFFSET24-hour time with seconds and abbreviated named offset13:30:23 EDT13:30:23 UTC−4
TIME_24_WITH_LONG_OFFSET24-hour time with seconds and full named offset13:30:23 Eastern Daylight Time13:30:23 heure d’été de l’Est
DATETIME_SHORTshort date & time10/14/1983, 1:30 PM14/10/1983 à 13:30
DATETIME_MEDabbreviated date & timeOct 14, 1983, 1:30 PM14 oct. 1983 à 13:30
DATETIME_MED_WITH_WEEKDAYabbreviated date & time with abbreviated weekdayFri, Oct 14, 1983, 1:30 PMven. 14 oct. 1983 à 13:30
DATETIME_FULLfull date and time with abbreviated named offsetOctober 14, 1983 at 1:30 PM EDT14 octobre 1983 à 13:30 UTC−4
DATETIME_HUGEfull date and time with weekday and full named offsetFriday, October 14, 1983 at 1:30 PM Eastern Daylight Timevendredi 14 octobre 1983 à 13:30 heure d’été de l’Est
DATETIME_SHORT_WITH_SECONDSshort date & time with seconds10/14/1983, 1:30:23 PM14/10/1983 à 13:30:23
DATETIME_MED_WITH_SECONDSabbreviated date & time with secondsOct 14, 1983, 1:30:23 PM14 oct. 1983 à 13:30:23
DATETIME_FULL_WITH_SECONDSfull date and time with abbreviated named offset with secondsOctober 14, 1983 at 1:30:23 PM EDT14 octobre 1983 à 13:30:23 UTC−4
DATETIME_HUGE_WITH_SECONDSfull date and time with weekday and full named offset with secondsFriday, October 14, 1983 at 1:30:23 PM Eastern Daylight Timevendredi 14 octobre 1983 à 13:30:23 heure d’été de l’Est

Intl

toLocaleString's behavior is affected by the DateTime's locale, numberingSystem, and outputCalendar properties. See the Intl section for more.

Formatting with tokens (strings for Cthulhu)

This section covers generating strings from DateTimes with programmer-specified formats.

Consider alternatives

You shouldn't create ad-hoc string formats if you can avoid it. If you intend for a computer to read the string, prefer ISO 8601. If a human will read it, prefer toLocaleString. Both are covered above. However, if you have some esoteric need where you need some specific format (e.g. because some other software expects it), then toFormat is how you do it.

toFormat

See DateTime#toFormat for the API signature. As a brief motivating example:

js
DateTime.fromISO('2014-08-06T13:07:04.054').toFormat('yyyy LLL dd'); //=> '2014 Aug 06'

The supported tokens are described in the table below.

Intl

All of the strings (e.g. month names and weekday names) are internationalized by introspecting strings generated by the Intl API. Thus the exact strings you get are implementation-specific.

js
DateTime.fromISO('2014-08-06T13:07:04.054')
  .setLocale('fr')
  .toFormat('yyyy LLL dd'); //=> '2014 août 06'

Note toFormat defaults to en-US. If you need the string to be internationalized, you need to set the locale explicitly like in the example above (or more preferably, use toLocaleString).

Escaping

You may escape strings using single quotes:

js
DateTime.now().toFormat("HH 'hours and' mm 'minutes'"); //=> '20 hours and 55 minutes'

Single quotes themselves can be escaped by doubling them:

js
DateTime.now().toFormat("MMM d ''yy"); //=> Apr 7 '25

Standalone vs format tokens

Some tokens have a "standalone" and "format" version. Some languages require different forms of a word based on whether it is part of a longer phrase or just by itself (e.g. "Monday the 22nd" vs "Monday"). Use them accordingly.

js
var d = DateTime.fromISO('2014-08-06T13:07:04.054').setLocale('ru');
d.toFormat('LLLL'); //=> 'август' (standalone)
d.toFormat('MMMM'); //=> 'августа' (format)

Macro tokens

Some of the formats are "macros", meaning they correspond to multiple components. These use the native Intl API and will order their constituent parts in a locale-friendly way.

js
DateTime.fromISO('2014-08-06T13:07:04.054').toFormat('ff'); //=> 'Aug 6, 2014, 1:07 PM'

The macro options available correspond one-to-one with the preset formats defined for toLocaleString.

Table of tokens

(Examples below given for 2014-08-06T13:07:04.054 considered as a local time in America/New_York).

Standalone tokenFormat tokenDescriptionExample
Smillisecond, no padding54
SSSmillisecond, padded to 3054
ufractional seconds, functionally identical to SSS054
uufractional seconds, between 0 and 99, padded to 205
uuufractional seconds, between 0 and 90
ssecond, no padding4
sssecond, padded to 2 padding04
mminute, no padding7
mmminute, padded to 207
hhour in 12-hour time, no padding1
hhhour in 12-hour time, padded to 201
Hhour in 24-hour time, no padding9
HHhour in 24-hour time, padded to 213
Znarrow offset+5
ZZshort offset+05:00
ZZZtechie offset+0500
ZZZZabbreviated named offsetEST
ZZZZZunabbreviated named offsetEastern Standard Time
zIANA zoneAmerica/New_York
ameridiemAM
dday of the month, no padding6
ddday of the month, padded to 206
cEday of the week, as number from 1-7 (Monday is 1, Sunday is 7)3
cccEEEday of the week, as an abbreviate localized stringWed
ccccEEEEday of the week, as an unabbreviated localized stringWednesday
cccccEEEEEday of the week, as a single localized letterW
LMmonth as an unpadded number8
LLMMmonth as a padded number08
LLLMMMmonth as an abbreviated localized stringAug
LLLLMMMMmonth as an unabbreviated localized stringAugust
LLLLLMMMMMmonth as a single localized letterA
yyear, unpadded2014
yytwo-digit year14
yyyyfour- to six- digit year, pads to 42014
Gabbreviated localized eraAD
GGunabbreviated localized eraAnno Domini
GGGGGone-letter localized eraA
kkISO week year, unpadded14
kkkkISO week year, padded to 42014
WISO week number, unpadded32
WWISO week number, padded to 232
iiLocal week year, unpadded14
iiiiLocal week year, padded to 42014
nLocal week number, unpadded32
nnLocal week number, padded to 232
oordinal (day of year), unpadded218
oooordinal (day of year), padded to 3218
qquarter, no padding3
qqquarter, padded to 203
Dlocalized numeric date9/4/2017
DDlocalized date with abbreviated monthAug 6, 2014
DDDlocalized date with full monthAugust 6, 2014
DDDDlocalized date with full month and weekdayWednesday, August 6, 2014
tlocalized time9:07 AM
ttlocalized time with seconds1:07:04 PM
tttlocalized time with seconds and abbreviated offset1:07:04 PM EDT
ttttlocalized time with seconds and full offset1:07:04 PM Eastern Daylight Time
Tlocalized 24-hour time13:07
TTlocalized 24-hour time with seconds13:07:04
TTTlocalized 24-hour time with seconds and abbreviated offset13:07:04 EDT
TTTTlocalized 24-hour time with seconds and full offset13:07:04 Eastern Daylight Time
fshort localized date and time8/6/2014, 1:07 PM
ffless short localized date and timeAug 6, 2014, 1:07 PM
fffverbose localized date and timeAugust 6, 2014, 1:07 PM EDT
ffffextra verbose localized date and timeWednesday, August 6, 2014, 1:07 PM Eastern Daylight Time
Fshort localized date and time with seconds8/6/2014, 1:07:04 PM
FFless short localized date and time with secondsAug 6, 2014, 1:07:04 PM
FFFverbose localized date and time with secondsAugust 6, 2014, 1:07:04 PM EDT
FFFFextra verbose localized date and time with secondsWednesday, August 6, 2014, 1:07:04 PM Eastern Daylight Time
Xunix timestamp in seconds1407287224
xunix timestamp in milliseconds1407287224054