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.
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())
}Optional: Sub and Duration.Milliseconds
You can measure elapsed time from the Unix epoch as a duration, then read whole milliseconds:
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())
}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):
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)
}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.

