英文:
Javascript toISOString time in Golang
问题
我正在尝试在Golang中生成ISO 8601时间戳。
在Golang中,可以使用以下代码:
time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
这将生成类似于JavaScript中toISOString()
的时间戳。
希望对你有帮助!
英文:
I'm trying to generate ISO 8601 timestamp in Golang.
Doing
time.Now().UTC().Format(time.RFC3339)
//2016-04-12T19:32:20Z
in Javascript
new Date().toISOString()
//2016-04-12T19:46:47.286Z
It appears the only difference is in JavaScript the time reports milliseconds, while Golang it produces it in seconds. I'd like to try and get these to be the same.
I've looked at time.RFC3339Nano
But that produces too much precision 2016-04-12T19:35:16.341032697Z
How can I get Golang to produce time that is equivalent to JavaScript's toISOString()
?
答案1
得分: 14
从查看 pkg/time 中定义常量的地方可以得知:
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
根据文档说明:
布局中使用的参考时间是特定的时间:
Mon Jan 2 15:04:05 MST 2006
要定义自己的格式,请写下参考时间以您的方式进行格式化的样子;
应该是这样的:
JavascriptISOString := "2006-01-02T15:04:05.999Z07:00"
time.Now().UTC().Format(JavascriptISOString)
英文:
From looking in pkg/time where the constants are defined
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
From the documentation:
> The reference time used in the layouts is the specific time:
> Mon Jan 2 15:04:05 MST 2006
> To define your own format, write down what the reference time would look like formatted your way;
It should be something like so:
JavascriptISOString := "2006-01-02T15:04:05.999Z07:00"
time.Now().UTC().Format(JavascriptISOString)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论