在Go中解析日期字符串 [英] Parsing date string in Go
本文介绍了在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 不。有人知道吗?)。
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屋!
查看全文