In Go, time.Time represents an instant with nanosecond precision and a location (time zone), while time.Duration is an int64 count of nanoseconds that represents an elapsed span. You create times with time.Now, time.Date or time.Parse, do arithmetic with Add, Sub and AddDate, and print them with Format, using the reference date Mon Jan 2 15:04:05 MST 2006 as the layout.
This guide covers creating, formatting, parsing and comparing times, working with durations, time zones, and the pitfalls that cause real bugs.
time.Date takes year, month, day, hour, minute, second, nanosecond and a *time.Location. Values outside the normal range are normalised, so day 32 of January becomes 1 February. The zero value of time.Time is January 1, year 1, UTC; test for it with IsZero() and do not compare with ==.
time.Now() includes a monotonic clock reading, which makes Since and Sub immune to wall-clock changes. Comparing with == on two Time values can therefore fail even for the same instant; use t1.Equal(t2), t1.Before(t2) and t1.After(t2).
Go does not use %Y-%m-%d. A layout is the reference time written the way you want the output to look: 2006-01-02 15:04:05. Each number stands for a field: 2006 year, 01 month, 02 day, 15 hour (24-hour), 04 minute, 05 second.
Predefined layouts such as time.RFC3339, time.DateOnly, time.TimeOnly and time.DateTime (the last three since Go 1.20) save you from typos. time.Parse assumes UTC when the input has no zone; use time.ParseInLocation to interpret it in a given zone.
A Duration is a typed int64 of nanoseconds. Build it with the constants rather than raw numbers.
The constants run from time.Nanosecond up to time.Hour. There is no Day constant because days vary with daylight saving time. Do not write time.Sleep(5 * n) where n is an int variable; convert it first with time.Duration(n) * time.Second.
| Task | Code |
|---|---|
| Add a duration | t.Add(48 * time.Hour) |
| Add calendar units | t.AddDate(0, 1, 0) (one month) |
| Difference of two times | t2.Sub(t1) returns a Duration |
| Time since or until | time.Since(t), time.Until(t) |
| Start of day | t.Truncate(24 * time.Hour) is UTC-based; use time.Date for local days |
| Compare | t1.Before(t2), t1.After(t2), t1.Equal(t2), t1.Compare(t2) |
AddDate works in calendar terms and normalises overflow: adding one month to 31 January gives 3 March (or 2 March in a leap year). Add(24 * time.Hour) adds exactly 24 hours, which across a daylight saving change is not the same wall-clock time tomorrow. Choose the one that matches your intent.
Compare (Go 1.20 and later) returns -1, 0 or 1, which is handy with slices.SortFunc.
LoadLocation reads the IANA time zone database from the system. In minimal containers it may be missing; import time/tzdata to embed a copy in your binary. Store and transmit times in UTC (or RFC 3339 with an offset), and convert to a zone only for display. Do not use fixed offsets for zones that observe daylight saving.
For recurring work, use time.NewTicker and always stop it. For a delay that can be cancelled, combine a timer or context.WithTimeout with select.
Prefer time.NewTimer or time.AfterFunc over time.After inside long-running loops on older Go versions, because an unfired After timer was not garbage collected until it fired. From Go 1.23, unreferenced timers and tickers are collected even if not stopped, though calling Stop is still good practice.
The reference time is Mon Jan 2 15:04:05 MST 2006, which is 01/02 03:04:05PM '06 -0700 when you count the fields as 1, 2, 3, 4, 5, 6, 7. Each part of a layout must use those exact numbers.
Compute t2.Sub(t1).Hours() / 24 for elapsed days. For calendar days, truncate both dates to midnight in the same location first, so daylight saving shifts do not skew the result.
Duration marshals as a number of nanoseconds in JSON. Store it as a string such as "30s" and use time.ParseDuration when you load it.
Yes. time.Time values are immutable value types, so you can share them freely.
Use time.Time for instants and time.Duration for spans. Format with the reference layout or predefined constants, compare with Equal, Before, After and Compare, and keep stored times in UTC. Build durations from constants, choose Add or AddDate deliberately, and stop tickers when you finish.