How to format a date or time in Go

Created
Modified

The layout string

Go doesn’t use yyyy-mm-dd layout to format a time. Instead, you format a special layout parameter.

2006-01-02 15:04:05.999999999 -0700 MST

package main

import (
  "fmt"
  "time"
)

func main() {

  // format current time
  s := time.Now().Format("2006-01-02 15:04:05")
  fmt.Println(s)

  // RFC3339
  s = time.Now().Format(time.RFC3339)
  fmt.Println(s)
}
2022-04-18 13:24:09
2022-04-18T13:24:09+09:00

The time package in the Golang standard library contains a bunch of useful constants that are used for formatting time.

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"
)


const (
  _                        = iota
  stdLongMonth             = iota + stdNeedDate  // "January"
  stdMonth                                       // "Jan"
  stdNumMonth                                    // "1"
  stdZeroMonth                                   // "01"
  stdLongWeekDay                                 // "Monday"
  stdWeekDay                                     // "Mon"
  stdDay                                         // "2"
  stdUnderDay                                    // "_2"
  stdZeroDay                                     // "02"
  stdUnderYearDay                                // "__2"
  stdZeroYearDay                                 // "002"
  stdHour                  = iota + stdNeedClock // "15"
  stdHour12                                      // "3"
  stdZeroHour12                                  // "03"
  stdMinute                                      // "4"
  stdZeroMinute                                  // "04"
  stdSecond                                      // "5"
  stdZeroSecond                                  // "05"
  stdLongYear              = iota + stdNeedDate  // "2006"
  stdYear                                        // "06"
  stdPM                    = iota + stdNeedClock // "PM"
  stdpm                                          // "pm"
  stdTZ                    = iota                // "MST"
  stdISO8601TZ                                   // "Z0700"  // prints Z for UTC
  stdISO8601SecondsTZ                            // "Z070000"
  stdISO8601ShortTZ                              // "Z07"
  stdISO8601ColonTZ                              // "Z07:00" // prints Z for UTC
  stdISO8601ColonSecondsTZ                       // "Z07:00:00"
  stdNumTZ                                       // "-0700"  // always numeric
  stdNumSecondsTz                                // "-070000"
  stdNumShortTZ                                  // "-07"    // always numeric
  stdNumColonTZ                                  // "-07:00" // always numeric
  stdNumColonSecondsTZ                           // "-07:00:00"
  stdFracSecond0                                 // ".0", ".00", ... , trailing zeros included
  stdFracSecond9                                 // ".9", ".99", ..., trailing zeros omitted

  stdNeedDate  = 1 << 8             // need month, day, year
  stdNeedClock = 2 << 8             // need hour, minute, second
  stdArgShift  = 16                 // extra argument in high bits, above low stdArgShift
  stdMask      = 1<<stdArgShift - 1 // mask out argument
)
There's no such thing as stdZeroHour, so no alternative behaviour for stdHour.

In Golang 1.17+ for fraction of seconds (.999 or .000) you can use , instead of . (,999 or ,000) but output is always with .

Using fmt.Sprintf Function

For example, to get the current date time as "yyyy-MM-dd HH:mm:ss", you can use these methods with fmt.Sprintf:

package main

import (
  "fmt"
  "time"
)

func main() {

  // format yyyy-MM-dd HH:mm:ss
  t := time.Now()
  s := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
  fmt.Println(s)
}
2022-04-18 13:34:02

This method isn't very effective. For 3-letter months, it does not work.

Related Tags

#format# #date# #time#