May 11, 2021
← Back to Blog  |  Home

Playing with date-fns

⏰ 2 min read

I used to work with moment, and then moved on to date-fns. Whenever I want to format a date other than yyyy-MM-dd, I have to go see the docs. So I'm adding my most used formatting strings here.

Year
yyyy    2021
yy      21
Month
M       1, 2, ..., 12
Mo      1st, 2nd, ..., 12th	
MM      01, 02, ..., 12
MMM     Jan, Feb, ..., Dec
MMMM    January, February, ..., December
Day of month
d       1, 2, ..., 31
do      1st, 2nd, ..., 31st
dd      01, 02, ..., 31
Hour (1 - 12)
h       1, 2, ..., 11, 12	
ho      1st, 2nd, ..., 11th, 12th
hh      01, 02, ..., 11, 12
Hour (0 - 23)
H       0, 1, 2, ..., 23	
Ho      0th, 1st, 2nd, ..., 23rd
HH      00, 01, 02, ..., 23
Minutes
m       0, 1, ..., 59	
mo      0th, 1st, ..., 59th
mm      00, 01, ..., 59
Seconds
s       0, 1, ..., 59	
so      0th, 1st, ..., 59th
ss      00, 01, ..., 59
AM/PM
a..aa   AM, PM	
aaa     am, pm	
aaaa    a.m., p.m.
aaaaa   a, p	
b..bb   AM, PM, noon, midnight	
bbb     am, pm, noon, midnight	
bbbb    a.m., p.m., noon, midnight
bbbbb   a, p, n, mi
Timezone (ISO-8601 w/ Z)
X       -08, +0530, Z	
XX      -0800, +0530, Z	
XXX     -08:00, +05:30, Z	
XXXX    -0800, +0530, Z, +123456
XXXXX   -08:00, +05:30, Z, +12:34:56
Timezone (ISO-8601 w/o Z)
x       -08, +0530, +00	
xx      -0800, +0530, +0000	
xxx     -08:00, +05:30, +00:00
xxxx     -0800, +0530, +0000, +123456	
xxxxx   -08:00, +05:30, +00:00, +12:34:56

Note: Full docs are available at date-fns official docs