Golang Datetime Cheatsheet from strftime*

25 Jul 21 13:31
3 minutes read

When I initially started working with Go the time format it seemed a bit clunky.

Maybe it’s because it’s a departure from the normal strftime, but also maybe it’s also because there’s no simple cheatsheet for the various date formatting options like you would see for example in the PHP documentation or in the Python documentation or to use the fancy language of the day, the Rust Chrono crate documentation.

The alternative is to read the source code, specifically the private constants.

That being said, here is a simple cheatsheet between the golang time format and php datetime format to make it easier to switch. No two strftime implementations are identical on what they support across various languages (especially if you look at 0 prefixed minutes or seconds), but most of the basic things like Y for full year are present.

golang characterphp characterDescriptionExample returned values
01dDay of the month, 2 digits with leading zeros01 to 31
MonDA textual representation of a day, three lettersMon through Sun
1jDay of the month without leading zeros1 to 31
Mondayl (lowercase ‘L’)A full textual representation of the day of the weekSunday through Saturday
JanuaryFA full textual representation of a month, such as January or MarchJanuary through December
01mNumeric representation of a month, with leading zeros01 through 12
JanMA short textual representation of a month, three lettersJan through Dec
1nNumeric representation of a month, without leading zeros1 through 12
2006YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
06yA two digit representation of a yearExamples: 99 or 03
pmaLowercase Ante meridiem and Post meridiemam or pm
PMAUppercase Ante meridiem and Post meridiemAM or PM
3g12-hour format of an hour without leading zeros1 through 12
15G24-hour format of an hour without leading zeros0 through 23
03h12-hour format of an hour with leading zeros01 through 12
N/AH24-hour format of an hour with leading zeros - not possible in Golang00 through 23
4iMinutes with leading zeros00 to 59
04N/AMinutes without leading zeros0 to 59
5sSeconds with leading zeros00 through 59
05N/ASeconds without leading zeros00 through 59
MSTeTimezone identifierExamples: UTCGMTAtlantic/Azores
-0700ODifference to Greenwich time (GMT) without colon between hours and minutesExample: +0200
Z07:00pDifference to Greenwich time (GMT) with colon between hours and minutesExample: +02:00
MSTTTimezone abbreviationExamples: ESTMDT …
-070000ZTimezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.-43200 through 50400

« Back to posts