Date and Time in Go¶
Go features the time package that provides functionality for measuring, parsing and formatting time.
TODO: Write about how Go uses a different approach than other languages for parsing and formatting dates (as Go does NOT use the MM-DD-YYYY style srtftime style common in many other languages).
Parse a date¶
Given the input date string “Friday, September 2024”, how to parse it into a time.Time
?
Let’s try:
layout := "Monday, January 2006"
date := "Friday, September 2024"
t, _ := time.Parse(layout, date)
fmt.Println(t)
//=> 2024-09-01 00:00:00 +0000 UTC
fmt.Println(t.Format("Monday, January 2, 2006"))
//=> Sunday, September 1, 2024
But according to the calendar, it should return a Friday, September 6.
$ cal 9 2024
September 2024
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
^
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
----
The problem is that our input date to be parsed cannot really be parsed. We have “Friday, September 2024”, but which Friday? There are four Fridays on September 2024.
In this case, it is parsing the month and year, and leaving the day alone, which defaults to the first day of that month.
So I guess that is the first lesson: dates to be parsed must be in a format that allows them to be correctly parsed.
layout := "2, January 2006"
date := "6, September 2024"
t, _ := time.Parse(layout, date)
fmt.Println(t)
//=> 2024-09-06 00:00:00 +0000 UTC
fmt.Println(t.Format("Monday, January 2, 2006"))
//=> Friday, September 6, 2024