Tuesday, May 21, 2024
HomeGolangDate and time format in Go (Golang) cheatsheet

Date and time format in Go (Golang) cheatsheet



Format date

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

bundle predominant

import (
  "fmt"
  "time"
)

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

Parse date

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

bundle predominant

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

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

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

Parse date with a timezone

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

bundle predominant

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

func predominant() {
  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.Deadly(err)
  }
  fmt.Println(t)
}

Reference format

To format or parse a date, you must specify the format of the enter or output date string. The Go language makes use of a particular date format format by which every a part of the date has an ordinal index:

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

  • 01 – month
  • 02 – day
  • 03 – hour (12h)
  • 04 – minute
  • 05 – second
  • 06 – 12 months
  • 07 – time zone offset

Helpful date and time layouts

Some layouts, not outlined within the time bundle, that are helpful in on a regular basis 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"
  // textual content date: March 23, 2022
  TextDate = "January 2, 2006"
  // textual content date with weekday: Wednesday, March 23, 2022
  TextDateWithWeekday = "Monday, January 2, 2006"
  // abbreviated textual content date: Mar 23 Wed
  AbbrTextDate = "Jan 2 Mon"
)

Predefined layouts

The built-in time/date layouts outlined within the time bundle:

const (
  Format      = "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"
  // Useful 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

Yr format
Go format Format Instance Description
2006 YYYY "2022" 4-digit 12 months
06 YY "22" Two-digit 12 months
Month format
Go format Format Instance Description
January MMMM "July" Full month identify
Jan MMM "Jul" Three-letter abbreviation of the month
01 MM "07" Two-digit month (with a number one 0 if vital)
1 M "7" At most two-digit month (and not using a main 0)
Day format
Go format Format Instance Description
Monday DDDD "Tuesday" Full weekday identify
Mon DDD "Tue" Three-letter abbreviation of the weekday
02 DD "08" Two-digit month day (with a number one 0 if vital)
_2 _D " 8" Two-character month day with a number one area if vital
2 D "8" At most two-digit month day (and not using a main 0)
002 ddd "074" Three-digit day of the 12 months (with a number one 0 if vital)
__2 __d " 74" Three-character day of the 12 months with a number one areas if vital

Time format

Hour format
Go format Format Instance Description
15 hh "17" Two-digit 24h format hour
03 hh "05" Two digit 12h format hour (with a number one 0 if vital)
3 h "5" At most two-digit 12h format hour (and not using a main 0)
PM am/pm "AM" AM/PM mark (uppercase)
pm am/pm "am" AM/PM mark (lowercase)
Minute format
Go format Format Instance Description
04 mm "07" Two-digit minute (with a number one 0 if vital)
4 m "7" At most two-digit minute (and not using a main 0)
Second format
Go format Format Instance Description
05 ss "09" Two-digit second (with a number one 0 if vital)
5 s "9" At most two-digit second (and not using a main 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 format Format Instance 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 however prints "Z" as an alternative of "+000000" for the UTC zone (ISO 8601 habits)
Z07:00:00 Z or ±hh:mm:ss "+01:00:00" Like -07:00:00 however prints "Z" as an alternative of "+00:00:00" for the UTC zone (ISO 8601 habits)
Z0700 Z or ±hhmm "+0100" Like -0700 however prints "Z" as an alternative of "+0000" for the UTC zone (ISO 8601 habits)
Z07:00 Z or ±hh:mm "+01:00" Like -07:00 however prints "Z" as an alternative of "+00:00" for the UTC zone (ISO 8601 habits)
Z07 Z or ±hh "+01" Like -07 however prints "Z" as an alternative of "+00" for the UTC zone (ISO 8601 habits)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments