Go Reference ยท Time, Math and Randomness
Go time.Now() function
Reference for Go time.Now(): the current local time with a monotonic clock reading, Unix timestamps, UTC conversion, rounding and testing without real time.
time.Now() returns the current moment as a time.Time, in the machine's local time zone. The value has nanosecond resolution (on most platforms the real precision is a bit lower) and includes a reading of the monotonic clock, which keeps running steadily even if someone changes the wall clock while your program is running.
Because the value changes on every call, code that calls time.Now() directly is hard to test. A common remedy is to pass the clock in as a parameter or a field.
now := time.Now() fmt.Println(now.Year(), now.Month(), now.Unix())
Try it
Real clock values change on every run, so these examples print only things that stay the same: relationships between times, not the times themselves.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Now() Time
| Name | Description |
|---|---|
| (none) | Now takes no parameters. |
Returns: the current Time in time.Local, carrying both the wall-clock and monotonic readings.
Errors / panics: none. The zone is Local, which is taken from the TZ environment variable or the system settings; in many containers that is UTC.
Useful follow-ups on the returned value:
| Expression | Result |
|---|---|
time.Now().UTC() |
The same instant in UTC. |
time.Now().Unix() |
Seconds since 1970-01-01 UTC. |
time.Now().UnixMilli() / UnixNano() |
Milliseconds / nanoseconds since the epoch. |
time.Now().Format(time.RFC3339) |
A portable string such as 2026-03-15T14:30:00+01:00. |
time.Now().In(loc) |
The same instant in another zone. |
Usage notes
- Measure durations with
time.Since(start), which uses the monotonic clock, rather than subtracting wall-clockUnix()numbers. - The monotonic reading is only inside the value that
Nowreturns. Operations likeRound(0),UTC(),In, formatting, JSON encoding and storing in a database drop it. - Do not compare with
==; useEqual. TwoNow()results stored in different places can describe the same instant and still differ in representation. - Prefer UTC for storage, logs and APIs.
time.Now().UTC()is an extremely common idiom. - Calls are cheap (tens of nanoseconds on Linux) but not free; do not call it in the hottest inner loop when one reading will do.
- Tests: accept a
func() time.Timeor a smallClockinterface, as in the example. For code with goroutines and timers, Go 1.25'stesting/synctestprovides a fake clock. - Seeding randomness with
time.Now().UnixNano()is no longer needed:math/randis seeded randomly since Go 1.20, andmath/rand/v2always is. - Time can jump backwards on the wall clock (NTP, manual changes, VM pauses); the monotonic reading protects
Sub,SinceandUntilfrom that.