Cheat sheetDev

Date format tokens

Date formatting tokens across strftime, Day.js/Moment and Excel side by side, plus the traps where the same letters mean different things.

Last updated

strftime (Python/C/Ruby)

TokenMeaning
%Y4-digit year, e.g. 2026.
%m2-digit month, 01 to 12.
%d2-digit day of month, 01 to 31.
%H2-digit hour, 24-hour clock, 00 to 23.
%M2-digit minute, 00 to 59.
%S2-digit second, 00 to 59.
%bAbbreviated month name, e.g. Sep.
%BFull month name, e.g. September.
%aAbbreviated weekday name, e.g. Fri.
%AFull weekday name, e.g. Friday.
%jDay of the year, 001 to 366.
%UWeek number of the year, Sunday as the first day.
%sUnix timestamp — seconds since 1970-01-01 UTC.
%zUTC offset, e.g. +0000 or -0500.

Day.js / Moment

TokenMeaning
YYYY4-digit year, e.g. 2026.
MM2-digit month, 01 to 12.
DD2-digit day of month, 01 to 31.
HH2-digit hour, 24-hour clock, 00 to 23.
mm2-digit minute, 00 to 59.
ss2-digit second, 00 to 59.
dddAbbreviated weekday name, e.g. Fri.
ddddFull weekday name, e.g. Friday.
AAM or PM, uppercase.
ZUTC offset with a colon, e.g. +00:00.

Excel / Sheets formats

TokenMeaning
yyyy4-digit year, e.g. 2026.
mm2-digit month when next to yyyy or dd; 2-digit minute when next to hh.
dd2-digit day of month, 01 to 31.
hh2-digit hour, 12-hour clock unless paired with AM/PM removed.
AM/PMAdd this to a time format to switch the hour to 12-hour and show AM or PM.
Custom format boxFormat Cells > Custom lets you combine any of these tokens in your own pattern.

The traps

TokenMeaning
MM vs mmIn JS date libraries, MM is month and mm is minute; in Excel it's the reverse — mm can mean either month or minute depending on context.
YYYY vs yyyyCase matters in code (YYYY is year, yyyy would be invalid in Moment/Day.js); Excel isn't case-sensitive at all, so yyyy and YYYY behave identically there.
hh vs HHhh is a 12-hour clock and needs an AM/PM token to be unambiguous; HH is 24-hour and never needs one.
%xLocale-dependent short date — the same format string prints different orders (MM/DD vs DD/MM) depending on the system locale.

Worth remembering

  • When two ecosystems reuse the same letters for different fields, code is the one place case is load-bearing — Excel format codes are case-insensitive, JS date libraries are not.
  • When in doubt, print a known date through the format and eyeball it — token tables (including this one) are a starting point, not a substitute for checking output.

Related in Cheat sheets