在 Go 中解析日期字符串 [英] Parsing date string in Go

查看:43
本文介绍了在 Go 中解析日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Go 中解析日期字符串 "2014-09-12T11:45:26.371Z".

I tried parsing the date string "2014-09-12T11:45:26.371Z" in Go.

代码

layout := "2014-09-12T11:45:26.371Z"
str := "2014-11-12T11:45:26.371Z"
t, err := time.Parse(layout , str)

我收到此错误:

解析时间2014-11-12T11:47:39.489Z":月份超出范围

parsing time "2014-11-12T11:47:39.489Z": month out of range

如何解析这个日期字符串?

How can I parse this date string?

推荐答案

使用描述的确切布局编号 这里 和一篇不错的博文这里.

Use the exact layout numbers described here and a nice blogpost here.

所以:

layout := "2006-01-02T15:04:05.000Z"
str := "2014-11-12T11:45:26.371Z"
t, err := time.Parse(layout, str)

if err != nil {
    fmt.Println(err)
}
fmt.Println(t)

给出:

>> 2014-11-12 11:45:26.371 +0000 UTC

我知道.脑洞大开.也是第一次抓住我.Go 只是不使用日期时间组件的抽象语法(YYYY-MM-DD),而是这些确切的数字(我认为 go 的第一次提交的时间 不,根据 this.有人知道吗?).

I know. Mind boggling. Also caught me first time. Go just doesn't use an abstract syntax for datetime components (YYYY-MM-DD), but these exact numbers (I think the time of the first commit of go Nope, according to this. Does anyone know?).

这篇关于在 Go 中解析日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆