We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.
To parse a date in Go, use the time.Parse() function from the time package:
packagemainimport("fmt""log""time")// date time format layoutconstYYYYMMDD="2006-01-02"funcmain(){s:="2022-03-23"t,err:=time.Parse(YYYYMMDD,s)iferr!=nil{log.Fatal(err)}fmt.Println(t)}
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 - month
02 - day
03 - hour (12h)
04 - minute
05 - second
06 - year
07 - 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-23YYYYMMDD="2006-01-02"// 24h hh:mm:ss: 14:23:20HHMMSS24h="15:04:05"// 12h hh:mm:ss: 2:23:20 PMHHMMSS12h="3:04:05 PM"// text date: March 23, 2022TextDate="January 2, 2006"// text date with weekday: Wednesday, March 23, 2022TextDateWithWeekday="Monday, January 2, 2006"// abbreviated text date: Mar 23 WedAbbrTextDate="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 zoneRFC850="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 zoneRFC3339="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 layout
Format
Example
Description
2006
YYYY
"2022"
Four-digit year
06
YY
"22"
Two-digit year
Month format
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)
Day format
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
Hour 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)
Minute format
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)
Second format
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
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)
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.