dougang1965 2016-04-12 19:50
浏览 141
已采纳

Java语言到Golang中的ISOString时间

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条回答 默认 最新

  • duanca3415 2016-04-12 19:59
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)