necroface necroface - 2 years ago 157
Javascript Question

Date.now() equivalent in Go

In JavaScript, I can assign:

var now = Date.now();


Then use
now
to calculate as a number variable

time.Time
type in Go doesn't seem to meet this demand. What is the Go equivalent of JavaScript's
Date.now()
?

Answer Source

Date.now() returns milliseconds since epoch UTC

The now() method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a Number.

To get that in Go, you can use:

time.Now().UTC().UnixNano() / 1e6
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download