Working with Dates and Time in Go: From Basics to Advanced Techniques
The Go programming language (Golang) offers a powerful and intuitive time package for working with dates and time. Unlike many other languages, Go uses a unique approach to formatting based on a reference date (Mon Jan 2 15:04:05 MST 2006). In this article, we will thoroughly examine all key aspects: from creating time objects to working with timers and time zones. The material will be useful for both beginners and experienced developers transitioning to Go.
1. Basics: Creating, Getting, and Displaying Time
The central type for working with time is time.Time. You can get the current moment using time.Now(). To create a specific date, use time.Date().
package main
import ( "fmt" "time")
func main() { // Current time now := time.Now() fmt.Println("Current time:", now)
// Creating a specific date (year, month, day, hour, min, sec, nanosec, Location) specific := time.Date(2025, time.January, 15, 10, 30, 0, 0, time.UTC) fmt.Println("Specific date:", specific)
// Extracting components fmt.Println("Year:", now.Year()) fmt.Println("Month:", now.Month()) fmt.Println("Day:", now.Day()) fmt.Println("Hour:", now.Hour()) fmt.Println("Minute:", now.Minute()) fmt.Println("Second:", now.Second()) fmt.Println("Weekday:", now.Weekday())}The program output will show the current time and its components. Note: time.Now() returns the time in the system's local time zone.
2. Formatting and Parsing: A Distinctive Feature of Go
In Go, date formatting is not done using characters like YYYY-MM-DD, but through a reference date: Mon Jan 2 15:04:05 MST 2006 (or 01/02 03:04:05PM '06 -0700). Remember this magic string — it looks like 1 2 3 4 5 6 -7.
- 1 — month (01, Jan, January)
- 2 — day (02, 2)
- 3 — hour in 12-hour format (03, 3)
- 4 — minute (04, 4)
- 5 — second (05, 5)
- 6 — year (06, 2006)
- -7 — time zone (MST, -0700, -07:00)
Formatting (Time → String)
now := time.Now()
// Standard formatsfmt.Println(now.Format("2006-01-02 15:04:05")) // 2025-01-15 10:30:45fmt.Println(now.Format("02.01.2006")) // 15.01.2025fmt.Println(now.Format(time.RFC3339)) // 2025-01-15T10:30:45Zfmt.Println(now.Format("Mon, 02 Jan 2006 15:04:05 MST")) // Wed, 15 Jan 2025 10:30:45 UTCParsing (String → Time)
// Parsing a string into time.TimedateStr := "2025-01-15 10:30:45"layout := "2006-01-02 15:04:05"parsedTime, err := time.Parse(layout, dateStr)if err != nil { panic(err)}fmt.Println("Parsed:", parsedTime)
// Parsing with time zonedateWithZone := "2025-01-15T10:30:45+03:00"parsedWithZone, _ := time.Parse(time.RFC3339, dateWithZone)fmt.Println("With zone:", parsedWithZone)If you need to parse a date in a specific time zone, use time.ParseInLocation().
3. Date Operations: Duration, Add, Sub, and Comparison
The time.Duration type represents a time interval. Go provides constants: Nanosecond, Microsecond, Millisecond, Second, Minute, Hour.
now := time.Now()
// Adding timefuture := now.Add(72 * time.Hour) // +3 daysfmt.Println("In 72 hours:", future)
// Subtracting timepast := now.Add(-30 * time.Minute) // 30 minutes agofmt