Datetime Format in Go

dnnrly

Pascal Dennerly

Posted on February 29, 2020

Datetime Format in Go

Datetime Format in Go

This ALWAYS confuses me in Go. How the bloody hell do you do you take a time.Time value and convert it to a string?

The documentation is here and here and here. There aren’t really any clues about where to start.

When you dig further in to the package and its code, you find this little gem. It defines the values of the different placeholders that are available in our formatting.

So lets start by giving a short example:

    t := time.Date(2018, 1, 5, 20, 43, 22, 123456789, time.Local)
    fmt.Println(t.Format("2006/01/02 15:04:05.999 -07"))
    fmt.Println(t.Format("3:04 PM, January 2 06"))

    // Output:
    // 2018/01/05 20:43:22.123 +00
    // 8:43 PM, January 5 18

You can see this example here

Slight aside - who knew that you could add examples in Go, that alsobecome tests! See Testable Examples in Go for details.

The complete

So here it is, the complete Go date/time string format…

Code Meaning Example
January It’s the full month name “January” / “February”
Jan The short month name “Jan” / “Feb”
1 Month as plain number “1” / “2” / “12”
01 Month as a 2 digits “01” / “02” / “12”
Monday The day of the week “Monday” / “Wednesday”
Mon The short day of the week “Mon” / “Wed”
2 Day of the month as a plain number “5” / “27”
02 Day of the month as a 2 digit number “05” / “27”
_2 Day of the month as a plain number but fixed width ” 5” / “27”
15 Hour 24h, 2 digits “02” / “15”
3 Hour 12h, plain number “2” / “15”
03 Hour 12h, 2 digits “02” / “15”
4 Minute as plain number “9” / “42”
04 Minute as 2 digits “09” / “42”
5 Seconds as plain number “8” / “25”
05 Seconds as 2 digits “08” / “25”
2006 Year as at least 4 digit number “0023” / “1999” / “25643” (WFT!)
06 Year as 2 digit number “23” / “99” / “43”
PM 12 hour period in upper case “AM” / “PM”
pm 12 hour period in lower case “am” / “pm”
MST Timezone short code “MST” / “CET” / “UTC”
Z0700 Timezone in hours and minutes offset (Z for UTC) ”-0600” / “+0100” / “Z”
Z070000 Timezone in hours, minutes and seconds offset (Z for UTC) ”-060000” / “+010000” / “Z”
Z07 Timezone in hours offset (Z for UTC) ”-06” / “+01” / “Z”
Z07:00 Timezone in colon seperated hours and minutes offset (Z for UTC) ”-06:00” / “+01:00” / “Z”
Z07:00:00 Timezone in colon seperated hours, minutes and seconds offset (Z for UTC) ”-06:00:00” / “+01:00:00” / “Z”
-0700 Timezone in hours and minutes, always numeric ”-0600” / “+0100” / “+0000”
-070000 Timezone in hours, minutes and seconds, always numeric ”-060000” / “+010000” / “+000000”
-0700 Timezone in hours and minutes, always numeric ”-0600” / “+0100” / “+0000”
-070:0:00 Timezone in colon seperated hours, minutes and seconds offset, always numeric ”-06:00:00” / “+01:00:00” / “+00:00:00”
.0/.00… Fractions of seconds, including trailing zeros ”.67” / “.900”
.9/.99… Fractions of seconds, including trailing zeros ”.67” / “.9”

Wow, there it is. That’s a lot of format!

But it’s still not easy to use.

💖 💪 🙅 🚩
dnnrly
Pascal Dennerly

Posted on February 29, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related