🕰️ Date and time format in Go cheatsheet
·5 mins
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"
01- month02- day03- hour (12h)04- minute05- second06- year07- time zone offset
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 #
| Go layout | Format | Example | Description |
|---|---|---|---|
| 2006 | YYYY | "2022" | Four-digit year |
| 06 | YY | "22" | Two-digit year |
| Go layout | Format | Example | Description |
|---|---|---|---|
| January | MMMM | "July" | Full month name |
| Jan | MMM | "Jul" | Three-letter abbreviation of the month |
| 01 | MM | "07" | Two-digit month (with a leading 0 if necessary) |
| 1 | M | "7" | At most two-digit month (without a leading 0) |
| Go layout | Format | Example | Description |
|---|---|---|---|
| Monday | DDDD | "Tuesday" | Full weekday name |
| Mon | DDD | "Tue" | Three-letter abbreviation of the weekday |
| 02 | DD | "08" | Two-digit month day (with a leading 0 if necessary) |
| _2 | _D | " 8" | Two-character month day with a leading space if necessary |
| 2 | D | "8" | At most two-digit month day (without a leading 0) |
| 002 | ddd | "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 #
| Go layout | Format | Example | Description |
|---|---|---|---|
| 15 | hh | "17" | Two-digit 24h format hour |
| 03 | hh | "05" | Two digit 12h format hour (with a leading 0 if necessary) |
| 3 | h | "5" | At most two-digit 12h format hour (without a leading 0) |
| PM | am/pm | "AM" | AM/PM mark (uppercase) |
| pm | am/pm | "am" | AM/PM mark (lowercase) |
| Go layout | Format | Example | Description |
|---|---|---|---|
| 04 | mm | "07" | Two-digit minute (with a leading 0 if necessary) |
| 4 | m | "7" | At most two-digit minute (without a leading 0) |
| Go layout | Format | Example | Description |
|---|---|---|---|
| 05 | ss | "09" | Two-digit second (with a leading 0 if necessary) |
| 5 | s | "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 #
| Go layout | Format | Example | Description |
|---|---|---|---|
| MST | TTT | "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 |
| Z070000 | Z or ±hhmmss | "+010000" | Like -070000 but prints "Z" instead of "+000000" for the UTC zone (ISO 8601 behavior) |
| Z07:00:00 | Z 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) |
| Z0700 | Z or ±hhmm | "+0100" | Like -0700 but prints "Z" instead of "+0000" for the UTC zone (ISO 8601 behavior) |
| Z07:00 | Z or ±hh:mm | "+01:00" | Like -07:00 but prints "Z" instead of "+00:00" for the UTC zone (ISO 8601 behavior) |
| Z07 | Z or ±hh | "+01" | Like -07 but prints "Z" instead of "+00" for the UTC zone (ISO 8601 behavior) |