Skip to main content
A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Go.

Getting Epoch Time

Use time.Now with Unix, UnixMilli, or UnixNano to get elapsed time since the Unix epoch:
now := time.Now()
fmt.Println(now)
// Output: 2026-03-03 10:15:30.123456789 +0000 UTC

Seconds Since Epoch

fmt.Println(now.Unix())
// Output: 1741001730

Milliseconds Since Epoch

fmt.Println(now.UnixMilli())
// Output: 1741001730123

Nanoseconds Since Epoch

fmt.Println(now.UnixNano())
// Output: 1741001730123456789

Converting Epoch to Time

Convert integer seconds or nanoseconds since the epoch into the corresponding time:

From Seconds

fmt.Println(time.Unix(now.Unix(), 0))
// Output: 2026-03-03 10:15:30 +0000 UTC

From Nanoseconds

fmt.Println(time.Unix(0, now.UnixNano()))
// Output: 2026-03-03 10:15:30.123456789 +0000 UTC

Epoch Time Methods

Unix
func() int64
Returns the number of seconds since January 1, 1970 UTC
UnixMilli
func() int64
Returns the number of milliseconds since January 1, 1970 UTC
UnixNano
func() int64
Returns the number of nanoseconds since January 1, 1970 UTC
time.Unix
func(sec int64, nsec int64) Time
Creates a Time from seconds and nanoseconds since the epoch

Use Cases

Timestamps

Store and compare event times

APIs

Exchange timestamps with external systems

Logging

Record when events occurred

Caching

Calculate expiration times

Time Formatting

Learn how to format and parse time values

Package Reference

Build docs developers (and LLMs) love