Parsing date string in Go

前端 未结 7 2079
小蘑菇
小蘑菇 2020-11-22 15:18

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

Code

layout := \"2014-09-12T11:45:26.371Z\"
str :=         


        
7条回答
  •  梦谈多话
    2020-11-22 15:48

    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
    

    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?).

提交回复
热议问题