英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论