Cookies management by TermsFeed Cookie Consent

🕰️ Date and time format in Go cheatsheet

cheatsheet time

Format date

To format a date in Go, use the time.Format() method of the Time struct:

package main

import (
  "fmt"
  "time"
)

func main() {
  t := time.Now()
  fmt.Println(t.Format(time.RFC3339))
}

Parse date

To parse a date in Go, use the time.Parse() function from the time package:

package main

import (
  "fmt"
  "log"
  "time"
)

// date time format layout
const YYYYMMDD = "2006-01-02"

func main() {
  s := "2022-03-23"
  t, err := time.Parse(YYYYMMDD, s)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(t)
}

Parse date with a timezone

The time.Parse() interprets a time as UTC. To parse a time at a specific location, use the time.ParseInLocation() function:

package main

import (
  "fmt"
  "log"
  "time"
)

func main() {
  s := "2022-03-23T07:00:00+01:00"
  loc, _ := time.LoadLocation("Europe/Berlin")
  t, err := time.ParseInLocation(time.RFC3339, s, loc)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(t)
}

Reference layout

To format or parse a date, you need to specify the layout of the input or output date string. The Go language uses a specific date layout format in which each part of the date has an ordinal index:

"01/02 03:04:05PM '06 -0700"

Useful date and time layouts

Some layouts, not defined in the time package, which are useful in everyday coding:

const (
  // YYYY-MM-DD: 2022-03-23
  YYYYMMDD = "2006-01-02"
  // 24h hh:mm:ss: 14:23:20
  HHMMSS24h = "15:04:05"
  // 12h hh:mm:ss: 2:23:20 PM
  HHMMSS12h = "3:04:05 PM"
  // text date: March 23, 2022
  TextDate = "January 2, 2006"
  // text date with weekday: Wednesday, March 23, 2022
  TextDateWithWeekday = "Monday, January 2, 2006"
  // abbreviated text date: Mar 23 Wed
  AbbrTextDate = "Jan 2 Mon"
)

Predefined layouts

The built-in time/date layouts defined in the time package:

const (
  Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
  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"
)

All date formatting strings

Date format

Year format
Go layoutFormatExampleDescription
2006YYYY"2022"Four-digit year
06YY"22"Two-digit year
Month format
Go layoutFormatExampleDescription
JanuaryMMMM"July"Full month name
JanMMM"Jul"Three-letter abbreviation of the month
01MM"07"Two-digit month (with a leading 0 if necessary)
1M"7"At most two-digit month (without a leading 0)
Day format
Go layoutFormatExampleDescription
MondayDDDD"Tuesday"Full weekday name
MonDDD"Tue"Three-letter abbreviation of the weekday
02DD"08"Two-digit month day (with a leading 0 if necessary)
_2_D" 8"Two-character month day with a leading space if necessary
2D"8"At most two-digit month day (without a leading 0)
002ddd"074"Three-digit day of the year (with a leading 0 if necessary)
__2__d" 74"Three-character day of the year with a leading spaces if necessary

Time format

Hour format
Go layoutFormatExampleDescription
15hh"17"Two-digit 24h format hour
03hh"05"Two digit 12h format hour (with a leading 0 if necessary)
3h"5"At most two-digit 12h format hour (without a leading 0)
PMam/pm"AM"AM/PM mark (uppercase)
pmam/pm"am"AM/PM mark (lowercase)
Minute format
Go layoutFormatExampleDescription
04mm"07"Two-digit minute (with a leading 0 if necessary)
4m"7"At most two-digit minute (without a leading 0)
Second format
Go layoutFormatExampleDescription
05ss"09"Two-digit second (with a leading 0 if necessary)
5s"9"At most two-digit second (without a leading 0)
.0, .00, …, .000000000.s".126284000"A fractional second (trailing zeros included)
.9, .99, …, .999999999.s".126284"A fractional second (trailing zeros omitted)

Time zone format

Time zone format
Go layoutFormatExampleDescription
MSTTTT"CEST"Abbreviation of the time zone
-070000±hhmmss"+010000"Numeric time zone offset with hours, minutes, and seconds
-07:00:00±hh:mm:ss"+01:00:00"Numeric time zone offset with hours, minutes, and seconds separated by colons
-0700±hhmm"+0100"Numeric time zone offset with hours and minutes
-07:00±hh:mm"+01:00"Numeric time zone offset with hours and minutes separated by colons
-07±hh"+01"Numeric time zone offset with hours
Z070000Z or ±hhmmss"+010000"Like -070000 but prints "Z" instead of "+000000" for the UTC zone (ISO 8601 behavior)
Z07:00:00Z or ±hh:mm:ss"+01:00:00"Like -07:00:00 but prints "Z" instead of "+00:00:00" for the UTC zone (ISO 8601 behavior)
Z0700Z or ±hhmm"+0100"Like -0700 but prints "Z" instead of "+0000" for the UTC zone (ISO 8601 behavior)
Z07:00Z or ±hh:mm"+01:00"Like -07:00 but prints "Z" instead of "+00:00" for the UTC zone (ISO 8601 behavior)
Z07Z or ±hh"+01"Like -07 but prints "Z" instead of "+00" for the UTC zone (ISO 8601 behavior)

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

⏳ Time difference between two dates in Go

shorts time

📅 YYYY-MM-DD date format in Go

Learn how to format date without time
introduction time

📝 Convert date or time to string in Go

shorts introduction time