Go / golang time.Now()。UnixNano()转换为毫秒? [英] Go / golang time.Now().UnixNano() convert to milliseconds?

查看:4166
本文介绍了Go / golang time.Now()。UnixNano()转换为毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下功能:

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

我需要更少的精度,只需要毫秒。

解决方案

只需将它分开即可:

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

下面是一个示例,您可以编译并运行以查看输出结果

  package main 

import(
time
fmt


func main(){
a := makeTimestamp()

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

func makeTimestamp()int64 {
返回time.Now()。UnixNano()/ int64(time.Millisecond)
}


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.

解决方案

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

这篇关于Go / golang time.Now()。UnixNano()转换为毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆