Go / golang time.Now().UnixNano() convert to milliseconds?

mconlin picture mconlin · Jun 9, 2014 · Viewed 124.3k times · Source

How can I get Unix time in Go in milliseconds?

I have the following function:

func makeTimestamp() int64 {
    return time.Now().UnixNano() % 1e6 / 1e3
}

I need less precision and only want milliseconds.

Answer

OneOfOne picture OneOfOne · Jun 9, 2014

Just divide it:

func makeTimestamp() int64 {
    return time.Now().UnixNano() / int64(time.Millisecond)
}

Here is an example that you can compile and run to see the output

package main

import (
    "time"
    "fmt"
)

func main() {
    a := makeTimestamp()

    fmt.Printf("%d \n", a)
}

func makeTimestamp() int64 {
    return time.Now().UnixNano() / int64(time.Millisecond)
}