Date and Time Formatting in Golang

Learn how to format a date in Go, using format constants or a custom format



In Go, the current time can be determined using time.Now(), provided by the time package.
t := time.Now()

This will print the system local time. Want UTC instead? append .UTC():

The time can be formatted using the time.Format() method:
t := time.Now().UTC()

Want the timestamp?
t := time.Now().Unix()


. . .

Use a custom format

An example of formatting with a custom format is:
package main

import (
"fmt"
"time"
)

func main() {
t := time.Now().UTC()
fmt.Println(t.Format("2006-01-02 15:04:05"))

}


That 2006-01-02 15:04:05 string looks strange, isn’t it? It’s not like it’s 2006 now!
But it will print
2009-11-10 23:00:00

If you’re new to Go, this will sound very strange. Here’s the explanation: Go’s time formatting is unique and different than what you would do in other languages.

Instead of having a conventional format to print the date, Go uses the reference date 20060102150405 which seems meaningless but actually has a reason, as it’s 1 2 3 4 5 6 in the Posix date command:
Mon Jan 2 15:04:05 -0700 MST 2006 0 1 2 3 4 5 6

The timezone in the middle would have been the 7 (not really sure why they didn’t pick Mon Jan 2 03:04:05 -0600 MST 2007, by the way)
Interesting historical reference: https://github.com/golang/go/issues/444
. . .

Use a format constants

Go provides in the time package some handy constants for commonly used 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" )

You can use them like this:
package main

import (
"fmt"
"time"
)

func main() {
t := time.Now()
fmt.Println("ANSIC: ", t.Format(time.ANSIC))

fmt.Println("RubyDate: ", t.Format(time.RubyDate))
fmt.Println("UnixDate: ", t.Format(time.UnixDate))

}

Never miss a post from Chris Gregori, when you sign up for Ednsquare.