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 
 YYYY-MM-DD 

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

Code

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

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

I got this error.

How to parse this date string?

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

so:

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)

gives:

>> 2014-11-12 11:45:26.371 +0000 UTC
YYYY-MM-DD

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