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

后端 未结 5 1470
栀梦
栀梦 2020-12-07 16:11

How can I get Unix time in Go in milliseconds?

I have the following function:

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


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 17:01

    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)
    }
    

提交回复
热议问题