Go Time Format
Preview Go time layouts, infer one from a sample, and convert to and from strftime.
Layout
t.Format(time.RFC3339) This is time.RFC3339 — use the constant rather than retyping the layout.
What each token means
2006Four-digit year202401Month, zero-padded0302Day, zero-padded0915Hour, 24-hour clock1404Minute, zero-padded0705Second, zero-padded09Z07:00Offset with colon, or Z when UTC-05:00Which layout parses this?
Paste a timestamp you already have and this works backwards to the layout that both parses and reproduces it.
strftime
2006-01-02 15:04:05%Y-%m-%dT%H:%M:%S%:zLayout constants in the time package
About Go time layouts
Go's time package does not use format specifiers. Instead of %Y-%m-%d you spell out a reference instant — Mon Jan 2 15:04:05 MST 2006 — so the layout for an ISO date is literally 2006-01-02. Each component has its own magic number, in the order 1 through 7: month, day, hour, minute, second, year, zone offset.
The same layout is used for both directions: t.Format(layout) and time.Parse(layout, value). That is convenient, but it means a layout that is subtly wrong will happily produce a wrong string in one direction and fail to parse real data in the other.
The reference time, component by component
2006year,06two-digit year01month zero-padded,1unpadded,JanandJanuaryby name02day zero-padded,2unpadded,_2space-padded15hour on a 24-hour clock;03and3on a 12-hour clock04minute,05second, both with unpadded4and5variants-0700and-07:00numeric offsets,MSTzone abbreviation
The mistakes that do not raise an error
Every one of these produces a plausible-looking string rather than failing, which is what makes them worth knowing:
- Using
03withoutPM.03is the 12-hour clock, so 14:07 and 02:07 both format as02. If you want a 24-hour clock the token is15, and it is the only one. Z07:00versus-07:00. The first prints a literalZwhen the offset is zero, which is what RFC 3339 requires. The second always prints+00:00. That single character is the difference between valid and invalid RFC 3339..000versus.999. Same precision, different output:.000keeps trailing zeros,.999strips them and omits the decimal point entirely when the fraction is zero.MSTdoes not round-trip. It prints a zone abbreviation, buttime.Parsecannot resolve an abbreviation back to a real location, so parsing your own output gives a zero offset. UseZ07:00or-0700for anything you intend to read back.
Coming from strftime
Most directives map across cleanly, but three gaps are worth knowing about. %-H has no equivalent — Go's only 24-hour token is 15, and it always zero-pads. There are no week-number tokens at all, so %U, %W and %V need t.ISOWeek() instead. And there is no epoch token; %s is t.Unix().
Is this accurate?
The formatter here is a reimplementation of Go's own Format, including the awkward tokenising rules — _2006 is a literal underscore followed by a year, and .04 is a literal dot followed by a minute rather than a one-digit fraction. It is checked against the real Go runtime across several thousand generated layout and instant combinations, in both directions, and is required to match byte for byte.