Skip to main content
Version: 2.0.2

Formatting

Usage

Formatter uses custom tokens. See list of formatter tokens

Escape strings by encasulating them in square brackets ([]).

epoch.format("YYYY [YYYY] MMMM [MMMM] Do [Do] LT [A]"); // 1970 YYYY January MMMM 1st Do 12:00 AM A

Alternatively, unmatched tokens will be left as is

epoch.format("YYYY년 MMM D일 ddd A h:mm"); // 1970년 1월 1일 목 오전 8:00 (Localization: KO)

Locale-specific formatters

// Same as calling the getter. [now.LL]
now.formatDate(); // July 14 2023

// Same as calling the getter. [now.ll]
now.formatDateShort(); // Jul 14 2023

// Same as calling the getter. [now.LLL]
now.formatDateTime(); // July 14 2023 03:50 AM

// Same as calling the getter. [now.lll]
now.formatDateTimeShort(); // Jul 14 2023 3:50 AM

// Same as calling the getter. [now.LLLL]
now.formatDateTimeWithWeekday(); // Friday, July 14 2023 03:50 AM

// Same as calling the getter. [now.llll]
now.formatDateTimeWithWeekdayShort(); // Fri, Jul 14 2023 3:50 AM

// Same as calling the getter. [now.LT]
now.formatTime(); // 3:50 AM

// Same as calling the getter. [now.LTS]
now.formatTimeWithSeconds(); // 3:50:46 AM
tip

Settings forceLocal: true converts DateTime to local one before formatting. Useful for parsed DateTimes and ones came from API.

now.formatTime(forceLocal: true);
now.toLocal().LTS;

See localization specific tokens

Formatter Tokens 🔑

TypeTokenExampleDescription
MonthM1 2 ... 11 12
Mo1st 2nd ... 11th 12th
MM01 02 ... 11 12
MMMJan Feb ... Nov Dec
MMMMJanuary February ... November December
Quarter of yearQ1 2 3 4
Qo1st 2nd 3rd 4th
Day of monthD1 2 ... 30 31
Do1st 2nd ... 30th 31st
DD01 02 ... 30 31
Day of yearDDD1 2 ... 364 365
DDDo1st 2nd ... 364th 365th
DDDD001 002 ... 364 365
Day of weekd1 2 ...6 7Moment.js uses `0-6`. However, we'll be using `1-7` to be in accordance with [DateTime]
d_o1st 2nd ... 6th 7th"do" is Dart language keyword
ddMo Tu ... Sa Su
dddMon Tue ... Sat Sun
ddddMonday ... Saturday Sunday
Day of week (ISO)e1 2 ... 6 7
Week of year (ISO)w1 2 ... 52 53
wo1st 2nd ... 52nd 53rd
ww01 02 ... 52 53
YearYY70 71 ... 29 30
YYYY1970 1971 ... 2029 2030
Era Yeary1 2 ... 2020 ...
EraNNBC ADAbbr era name
NNNNBefore Christ, Anno DominiFull era name
NNNNNBC ADNarrow era name
Week yeargg70 71 ... 29 30
gggg1970 1971 ... 2029 2030
AM/PMAAM PMUPPERCASE
aam pmlowercase
HourH0 1 ... 22 23
HH00 01 ... 22 23
h1 2 ... 11 12
hh01 02 ... 11 12
k1 2 ... 23 24
kk01 02 ... 23 24
Minutem0 1 ... 58 59
mm00 01 ... 58 59
Seconds0 1 ... 58 59
ss00 01 ... 58 59
Fractional secondS0 1 ... 8 9
SS00 01 ... 98 99
SSS000 001 ... 998 999
SSSS0000 0001 ... 9998,9999
SSSSS00000 00001 ... 99998,99999
SSSSSS000000 000001 ... 999998,999999
TimezoneZ-07:00 -06:00 ... +06:00 +07:00
ZZ-0700 -0600 ... +0600 +0700
Timezone nameZZZReturns [DateTime.timeZoneName], results may not be consistent across platforms
Unix timestamp in secondsX1654063960
Unix timestampx1654063974620

Localization specific tokens

Following fromatters are specific to each localization, and are implemented at per-localization level.

TokenExampleDescription
l9/4/1986Date (in local format, shorter)
L09/04/1986Date (in local format)
llSep 4 1986Month name, day of month, year (shorter)
LLSeptember 04 1986Month name, day of month, year
lllSep 4 1986 8:30 PMMonth name, day of month, year, time
LLLSeptember 04 1986 8:30 PMMonth name, day of month, year, time
llllThu, Sep 4 1986 8:30 PMDay of week, month name, day of month, year, time (shorter)
LLLLThursday, September 04 1986 8:30 PMDay of week, month name, day of month, year, time
LT8:30 PMTime (without seconds)
LTS8:30:00 PMTime (with seconds)