The Go time package handles dates, clocks, durations and timers in one place. In this post, you will create dates and format them with Go’s unusual layout. Then you will do date maths and work with time zones. Each idea comes with a short example that you can run yourself.
You already used time.Sleep and time.After in the Go select statement. Durations also appeared in Go context for beginners, so now let us look at time properly.
What Is the Go Time Package?
The time package gives you two main types. A time.Time is a moment, such as 19 August 2026 at 9:30. A time.Duration is a length of time, such as 90 minutes. Most work mixes the two, for example “now plus 30 days”.
t := time.Date(2026, time.August, 19, 9, 30, 0, 0, time.UTC)
fmt.Println(t) // 2026-08-19 09:30:00 +0000 UTC
fmt.Println(t.Year(), t.Month(), t.Day(), t.Weekday()) // 2026 August 19 Wednesday
Call time.Now() to get the current moment. However, examples here use a fixed date, so your output matches.
Formatting Dates with the Reference Layout
Most languages use codes like YYYY-MM-DD. Go instead uses one special reference date: Monday, 2 January 2006 at 15:04:05. You write that exact date in the shape you want, and Go copies the shape. The diagram below shows how each part maps.

Each part of the reference time stands for one piece of the output: 2006 is the year, 01 the month, 02 the day and 15:04 the time.
fmt.Println(t.Format("2006-01-02")) // 2026-08-19
fmt.Println(t.Format("02 Jan 2006, 3:04 PM")) // 19 Aug 2026, 9:30 AM
fmt.Println(t.Format(time.RFC3339)) // 2026-08-19T09:30:00Z
fmt.Println(t.Format("YYYY-MM-DD")) // YYYY-MM-DD
The last line is a classic trap. Go does not know YYYY, so it prints the letters unchanged. Here is a handy memory trick. The parts count up: month 01, day 02, hour 03 (15), minute 04, second 05 and year 06.
Parsing Text into Dates
p, err := time.Parse("2006-01-02 15:04", "2026-08-25 18:45")
fmt.Println(p, err) // 2026-08-25 18:45:00 +0000 UTC <nil>
_, err = time.Parse("2006-01-02", "25-08-2026")
fmt.Println(err)
// parsing time "25-08-2026" as "2006-01-02": cannot parse "25-08-2026" as "2006"
time.Parse uses the same layout idea in reverse, which keeps the Go time package consistent. So the layout must match the input shape exactly. Also, always check the error, because user input is often in the wrong format.
Durations and Date Maths in the Go Time Package
d := 90 * time.Minute
fmt.Println(d, d.Hours()) // 1h30m0s 1.5
later := t.Add(36 * time.Hour)
fmt.Println(later.Format("Mon 02 Jan 15:04")) // Thu 20 Aug 21:30
fmt.Println(later.Sub(t)) // 36h0m0s
fmt.Println(t.AddDate(0, 1, 0).Format("2006-01-02")) // 2026-09-19
fmt.Println(later.After(t)) // true
Add moves a moment by a duration, and Sub gives the gap between two moments. For calendar steps like “one month later”, use AddDate instead. In addition, compare moments with Before, After and Equal rather than ==.
Measuring How Long Code Takes
start := time.Now()
doWork()
fmt.Println("took", time.Since(start))
time.Since(start) is a short way to write time.Now().Sub(start). For example, it is perfect for logging slow requests.
Time Zones with the Go Time Package
loc, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
log.Fatal(err)
}
fmt.Println(t.In(loc).Format("2006-01-02 15:04 MST")) // 2026-08-19 15:00 IST
With the Go time package, store times in UTC. Then convert to a local zone only when you show them to a person. This simple rule prevents most time zone bugs. On machines without zone data, such as tiny containers, add import _ "time/tzdata" to embed it.
Timers and Tickers
timer := time.NewTimer(20 * time.Millisecond)
<-timer.C
fmt.Println("timer fired")
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
A timer fires once, while a ticker fires again and again. So always call Stop on a ticker when you finish, or it keeps running in the background.
Common Mistakes with the Go Time Package
| Problem you see | Why it happens | How to fix it |
|---|---|---|
| Output shows YYYY-MM-DD literally | Because go uses the 2006 layout, not letter codes. | So write the layout as “2006-01-02”. |
| cannot parse “25-08-2026” as “2006” | Because the layout does not match the input. | So make the layout follow the input shape. |
| unknown time zone Asia/Kolkata | Because the machine has no zone database. | So import time/tzdata. |
| Two equal times compare as different | Because == also compares the location. | So use t1.Equal(t2). |
Date bugs are common in real projects and interviews alike. So practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they stay accurate and up to date.
Quick Recap
- The Go time package uses
time.Timefor moments andtime.Durationfor lengths. - Format and parse with the reference time 2006-01-02 15:04:05.
- Use
Add,Sub,AddDateandtime.Sincefor date maths. - Store in UTC and convert with
Infor display. - Stop tickers when you no longer need them.
What’s Next?
You can now handle dates and durations. Next, we will search and transform text with regular expressions and the regexp package.
Next lesson: Go Regular Expressions with the regexp Package


