Introduction to Working with Dates and Time in Go
Working with dates and time is one of the key tasks in any programming language. The Go language (Golang) provides a powerful and intuitive time package that allows you to perform all basic operations: getting the current time, parsing strings, formatting, calculating differences, creating timers, and much more. Unlike some other languages, Go uses a unique reference time format, which makes the code more readable and predictable. In this article, we will thoroughly examine the main capabilities of the time package using practical examples.
Basic Types and Getting the Current Time
The time.Time Type
The central type in the time package is the time.Time structure. It stores information about seconds, nanoseconds, time zone, and a monotonic counter (for precise measurements).
Getting the Current Moment
To get the current time, the time.Now() function is used. It returns the local time based on system settings.
package main
import ( "fmt" "time")
func main() { now := time.Now() fmt.Println("Current time:", now) 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("Nanosecond:", now.Nanosecond()) fmt.Println("Weekday:", now.Weekday())}Creating Time Manually
You can create a specific moment in time using the time.Date() function. It takes year, month, day, hour, minute, second, nanosecond, and time zone.
package main
import ( "fmt" "time")
func main() { // March 15, 2025, 14:30:00, UTC specificDate := time.Date(2025, time.March, 15, 14, 30, 0, 0, time.UTC) fmt.Println("Created date:", specificDate)}Parsing and Formatting Dates
Go Reference Format
Go uses a unique approach to formatting: instead of codes like YYYY-MM-DD, it uses the reference date Mon Jan 2 15:04:05 MST 2006 (or abbreviated as 2006-01-02T15:04:05Z07:00). This means that to specify the year you write 2006, for the month — 01, for the day — 02, for the hour — 15 (or 03 for 12-hour format), for the minute — 04, for the second — 05.
Formatting Time to String
The Format() method converts time.Time into a string according to a given template.
package main
import ( "fmt" "time")
func main() { now := time.Now() fmt.Println("Standard format:", now.Format("2006-01-02 15:04:05")) fmt.Println("Russian format:", now.Format("02.01.2006 15:04")) fmt.Println("Date only:", now.Format("02 Jan 2006")) fmt.Println("12-hour format:", now.Format("03:04:05 PM"))}Parsing a String into a Date
The time.Parse() function converts a string into time.Time. It is important that the string matches the template.
package main
import ( "fmt" "time")
func main() { dateStr := "2025-03-15 14:30:00" layout := "2006-01-02 15:04:05" parsedTime, err := time.Parse(layout, dateStr) if err != nil { fmt.Println("Parsing error:", err) return } fmt.Println("Parsed time:", parsedTime) fmt.Println("Weekday:", parsedTime.Weekday())}Parsing with Time Zone
For strings with a time zone, use time.ParseInLocation() or specify the offset in the template (Z07:00).
package main
import ( "fmt" "time")