How to get current time in milliseconds using Go language?

huangapple go评论73阅读模式
英文:

How to get current time in milliseconds using Go language?

问题

我正在尝试使用Go语言获取当前时间的毫秒数。以下是代码:

package main
import "fmt"
import "time"
func main() {

    now := time.Now()
    secs := now.Unix()
    nanos := now.UnixNano()
    fmt.Println(now)

    millis := nanos / 1000000
    fmt.Println(millis)

}

当我在提到的网站上运行上述代码时,我得到的输出如下:

2009-11-10 23:00:00 +0000 UTC
1257894000000

我不明白为什么我没有得到当前日期作为结果?我在另一个网站https://www.epochconverter.com/上尝试了相同的代码,并得到了正确的结果,如下所示:

Time Now is : 
1496230018

有人可以确认一下问题是与我的代码有关还是网站显示了错误的结果吗?

谢谢。

英文:

I am trying to get the current time in millisecond using Go language https://golang.org/#

package main
import "fmt"
import "time"
func main() {

    now := time.Now()
    secs := now.Unix()
    nanos := now.UnixNano()
    fmt.Println(now)

    millis := nanos / 1000000
    fmt.Println(millis)

}

When I run the above code using the mentioned website the output I get is following:

2009-11-10 23:00:00 +0000 UTC
1257894000000

I am not able to understand why I am not getting current date as result? I tried the same code on another website https://www.epochconverter.com/ and got the correct result which is following :

Time Now is : 
1496230018

Can someone confirm if the problem is related to my code or is the website that's showing the wrong result?

Thanks

答案1

得分: 10

看起来你正在Go Playground(play.golang.org)上运行代码。Go Playground上的时间是固定的,建议你在本地运行代码。

英文:

It seems like you're running this on the Go playground (play.golang.org). The time is fixed on the Go playground, try running it locally instead.

huangapple
  • 本文由 发表于 2017年5月31日 20:24:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/44284754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定