Search code examples
javascriptnode.jsdatetimego

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() ?


Solution

  • 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
    

    or since Go version 1.17

    time.Now().UTC().UnixMilli()