Get Current Time in Milliseconds in Go (UnixMilli and Alternatives)

Convert time.Time to Unix milliseconds with UnixMilli, derive ms from UnixNano, or use Duration.Milliseconds after Sub; know int64 range limits from the time package docs.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Get Current Time in Milliseconds in Go (UnixMilli and Alternatives)

The time package exposes wall-clock instants as time.Time. To get a Unix timestamp in seconds you use Unix(); for milliseconds since the epoch, use UnixMilli() (Go 1.17+). You can also derive milliseconds from UnixNano() or from a time.Duration via Milliseconds(). For arithmetic on Time values, see subtracting a duration from time and the zero value of time.Time.

Tested with Go 1.24 on Linux.


Use UnixMilli for epoch milliseconds

UnixMilli returns t as an int64 count of milliseconds since January 1, 1970 UTC. It does not depend on the location associated with t.

Run the program: you should see the current instant printed, a large millisecond epoch value, and 1672531200000 for 2023-01-01 UTC.

go
package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println("time:", now)
	fmt.Println("Unix time in milliseconds:", now.UnixMilli())

	utc := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(utc.UnixMilli())
}
Output

Optional: Sub and Duration.Milliseconds

You can measure elapsed time from the Unix epoch as a duration, then read whole milliseconds:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	d := now.Sub(time.Unix(0, 0))
	fmt.Println(d.Milliseconds())

	utc := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(utc.Sub(time.Unix(0, 0)).Milliseconds())
}
Output

For wall-clock epoch milliseconds, UnixMilli is usually clearer than Sub(time.Unix(0,0)).Milliseconds().


From UnixNano to milliseconds

Divide nanoseconds by 1e6, or by int64(time.Millisecond / time.Nanosecond):

go
package main

import (
	"fmt"
	"time"
)

func main() {
	ms := time.Now().UnixNano() / int64(time.Millisecond)
	fmt.Println(ms)

	utc := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(utc.UnixNano() / 1e6)
}
Output
NOTE
UnixNano is documented as undefined if the instant cannot be represented as an int64 nanosecond count. Prefer UnixMilli when millisecond resolution is enough.

Summary

Use time.Now().UnixMilli() for current time in milliseconds since the Unix epoch. UnixNano()/1e6 and duration-based Milliseconds() are alternatives when you already have nanoseconds or a Duration. Older material sometimes misspells UnixMilli as “UnixMulli”; the correct identifier is UnixMilli.


References


Frequently Asked Questions

1. How do I get the current time in milliseconds in Go?

Call time.Now().UnixMilli() for the number of milliseconds since January 1, 1970 UTC. It returns int64 and is available from Go 1.17 onward.

2. What is the difference between UnixMilli and UnixNano?

UnixMilli counts milliseconds since the epoch; UnixNano counts nanoseconds. You can divide UnixNano by 1e6 to get milliseconds, but UnixMilli is clearer when you only need millisecond resolution.

3. Is now.Sub(time.Unix(0,0)).Milliseconds() the same as UnixMilli?

For instants representable as both duration-from-epoch and Unix time, they match the millisecond count since the epoch. Prefer UnixMilli for readability unless you are working purely with durations.

4. When is UnixNano undefined?

Per the time package, UnixNano is undefined if the instant cannot be represented as int64 nanoseconds (roughly before year 1678 or after 2262). UnixMilli has a similar documented range limit for int64 microseconds-scale extremes.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …