DEV Community

Alexandru Bucur
Alexandru Bucur

Posted on • Originally published at alexandrubucur.com on

Golang Datetime Cheatsheet from strftime*

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 character php character Description Example returned values
Day Day --- ---
01 d Day of the month, 2 digits with leading zeros 01 to 31
Mon D A textual representation of a day, three letters Mon through Sun
1 j Day of the month without leading zeros 1 to 31
Monday l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
Month Month --- ---
January F A full textual representation of a month, such as January or March January through December
01 m Numeric representation of a month, with leading zeros 01 through 12
Jan M A short textual representation of a month, three letters Jan through Dec
1 n Numeric representation of a month, without leading zeros 1 through 12
Year Year --- ---
2006 Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
06 y A two digit representation of a year Examples: 99 or 03
Time Time --- ---
pm a Lowercase Ante meridiem and Post meridiem am or pm
PM A Uppercase Ante meridiem and Post meridiem AM or PM
3 g 12-hour format of an hour without leading zeros 1 through 12
15 G 24-hour format of an hour without leading zeros 0 through 23
03 h 12-hour format of an hour with leading zeros 01 through 12
N/A H 24-hour format of an hour with leading zeros - not possible in Golang 00 through 23
4 i Minutes with leading zeros 00 to 59
04 N/A Minutes without leading zeros 0 to 59
5 s Seconds with leading zeros 00 through 59
05 N/A Seconds without leading zeros 00 through 59
Timezone Timezone --- ---
MST e Timezone identifier Examples: UTCGMTAtlantic/Azores
-0700 O Difference to Greenwich time (GMT) without colon between hours and minutes Example: +0200
Z07:00 p Difference to Greenwich time (GMT) with colon between hours and minutes Example: +02:00
MST T Timezone abbreviation Examples: ESTMDT ...
-070000 Z Timezone 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

Top comments (0)