DateTime formatting and time zones
The pattern letters, style tokens, locales and time zone IDs that DateTime accepts when it formats, parses and converts dates in server scripts.
This page lists what format(pattern), DateTime(text, pattern), withLang() and withZone() accept. Server-side DateTime formats with Joda-Time’s DateTimeFormat, so patterns follow Joda-Time syntax, not Moment.js syntax.
The examples use 6 April 1968 at 18:25:32.678, a Saturday.
Pattern letters
Letters are case-sensitive: D is the day of the year and d the day of the month. Repeat a number letter to pad it with zeros. Four or more text letters give the full form, fewer give the short form.
| Token | Output | Example |
|---|---|---|
yyyy |
Year | 1968 |
yy |
Year, two digits | 68 |
M |
Month number | 4 |
MM |
Month number, two digits | 04 |
MMM |
Month name, short | Apr |
MMMM |
Month name, full | April |
d |
Day of the month | 6 |
dd |
Day of the month, two digits | 06 |
D |
Day of the year | 97 |
E |
Day of the week, short | Sat |
EEEE |
Day of the week, full | Saturday |
e |
Day of the week number, Monday is 1 | 6 |
w |
Week of the week-based year | 14 |
H |
Hour of the day, 0 to 23 | 18 |
HH |
Hour of the day, two digits | 18 |
h |
Hour of the half-day, 1 to 12 | 6 |
hh |
Hour of the half-day, two digits | 06 |
a |
Half of the day | PM |
mm |
Minute, two digits | 25 |
ss |
Second, two digits | 32 |
SSS |
Milliseconds | 678 |
Z |
Offset from UTC | +1000 |
ZZ |
Offset from UTC with a colon | +10:00 |
ZZZ |
Time zone ID | Australia/Brisbane |
G |
Era | AD |
'text' |
Literal text | yyyy'T'HH gives 1968T18 |
'' |
A single quote | ' |
Style tokens
A style token on its own picks the locale’s own date and time layout instead of a pattern. The Example column gives the Joda-Time date and time styles each token maps to.
| Token | Output | Example |
|---|---|---|
LT |
Time, short style | Date none, time short |
LTS |
Time, medium style | Date none, time medium |
l |
Date, short style | Date short, time none |
L |
Date, medium style | Date medium, time none |
ll |
Date, medium style | Date medium, time none |
LL |
Date, long style | Date long, time none |
lll |
Date and time, medium date | Date medium, time short |
LLL |
Date and time, long date | Date long, time short |
llll |
Date and time, full date | Date full, time short |
LLLL |
Date and time, full date | Date full, time short |
Locales
Text tokens, style tokens and fromNow() follow the object’s locale. A new DateTime takes the current user’s locale, or the system.locale application property when there is no user. Change it with withLang(locale) on one object or DateTime.withLang(locale) for the rest of the script.
| Token | Output | Example |
|---|---|---|
| Language only | An ISO 639 language code | "fr" |
| Language and country | A language code and an ISO 3166 country code, with a hyphen or an underscore | "fr-FR" or "fr_FR" |
Time zones
A new DateTime takes the time zone on the current user’s record. When there is no user or the user has none, it takes the system.timezone application property. withZone() and toLocalDateTime() take a time zone ID from the IANA time zone database, in the form Area/Location. A space in an ID is read as an underscore. An unknown ID raises an error.
| Token | Output | Example |
|---|---|---|
| Area/Location ID | A region’s time zone, with its daylight saving rules | "Europe/Paris", "America/New_York" |
"UTC" |
Coordinated Universal Time, with no daylight saving | "UTC" |
Always set the zone you mean before you compare days or build a date. The zone decides daylight saving and which day an instant falls on.