doulanyan6455
2018-11-06 22:58
浏览 63
已采纳

将Python解析为Golang-DateTime

I've been attempting to get a specific string output from pyhtons datetime (2006-01-02T15:04:05.000Z), so that I can easily parse it in golang using time.Parse.

I tried (in python)

datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')

Which will give me something like this: "2018-11-06T22:48:50.002750Z"

And when I try to parse it like this in Golang:

dtLayout := "2006-01-02T15:04:05.000Z"
dateStr := "2018-11-06T22:48:50.002750Z"

parsedDate, err := time.Parse(dtLayout, dateStr)
if err != nil {
    if err != nil {
        log.Printf("error: %v", err)
    }

I get this error:

2018/11/06 16:49:11 error: parsing time "2018-11-06T22:48:50.002750Z" as "2006-01-02T15:04:05.000Z": cannot parse "750Z" as "Z"

There's probably an easy way to do this using just milliseconds, but I'm stubborn and feel like I'm close.

图片转代码服务由CSDN问答提供 功能建议

我一直在尝试从pyhtons datetime中获取特定的字符串输出 (2006-01-02T15:04 :05.000Z),这样我就可以使用time.Parse在golang中轻松解析它。

我尝试过(在python中)

  datetime.utcnow()。strftime('%Y-%m-%dT%H  :%M:%S.%fZ')
   
 
 

这会给我这样的信息:“ 2018-11-06T22:48:50.002750Z”

,当我尝试在Golang中像这样解析它时:

  dtLayout:=“ 2006-01-02T15:04:05.000Z”  
dateStr:=“ 2018-11-06T22:48:50.002750Z” 
 
parsedDate,err:= time.Parse(dtLayout,dateStr)
if err!= nil {
如果err!= nil {
日志 .Printf(“错误:%v”,错误)
} 
   
 
 

我收到此错误:

2018/11/06 16:49:11错误:将时间“ 2018-11-06T22:48:50.002750Z”解析为“ 2006-01-02T15:04:05.000Z”:无法将“ 750Z”解析为“ Z“

大概只有几毫秒就能做到这一点,但是我很固执,觉得自己已经接近了。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题