Essential Go Time and date  Edit on GitHub      File Issue

Format time

Most languages inherited time formatting method from strftime function C library which uses somewhat cryptic format strings like %Y-%m-%d.

Go designers came up with arguably more intuitive way of time parsing and formatting where you show a template of how you want the result to look like:

t := time.Date(2017, 9, 4, 3, 38, 45, 0, time.UTC)
fmt.Println(t.Format("2006-02-01 15:04:05.000 MST"))
fmt.Println(t.Format("2006-02-1 15pm"))
fmt.Println(t.Format("Jan 06 Mon 2 01"))
fmt.Println(t.Format("January 6 Mon 2 1"))
fmt.Println(t.Format("Month: Jan '1', '01', _2"))
2017-04-09 03:38:45.000 UTC
2017-04-9 03am
Sep 17 Mon 4 09
September 6 Mon 4 9
Month: Sep '9', '09',  4

Formatting string is an arbitrary string with some parts being replaced by the data from time.Time value:

templatemeaning
2006, 064 or 2 digit year
2month, 1-12
1day, 1-31
15hour
am, PMshow hour in am/pm format
4minute
5second
MSTstring time zone
-0700numeric time zone
Jan, Januaryshort or long month name
Mon, Mondayshort or long day name

Days, months, years, hours, minutes and seconds can be zero-padded by adding 0 to format number. It’ll only be shown for numbers < 10. 02 means zero-padded month i.e. 04 or 11.

Some values can be space-padded. _2 will be 4 or 11.

Package time also defines constants for some well-known formats for date/time formatting e.g. time.RFC822 is date format defined in RFC 822 which is date format in e-mail messages.

Here’s a full list of pre-defined formats:

const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
)

What if you prefer strftime style of formatting time? That’s available too.

  ↑ ↓ to navigate     ↵ to select     Esc to close