Parsing date string in Go

前端 未结 7 2037
小蘑菇
小蘑菇 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:50

    The layout to use is indeed "2006-01-02T15:04:05.000Z" described in RickyA's answer.
    It isn't "the time of the first commit of go", but rather a mnemonic way to remember said layout.
    See pkg/time:

    The reference time used in the layouts is:

    Mon Jan 2 15:04:05 MST 2006
    

    which is Unix time 1136239445.
    Since MST is GMT-0700, the reference time can be thought of as

     01/02 03:04:05PM '06 -0700
    

    (1,2,3,4,5,6,7, provided you remember that 1 is for the month, and 2 for the day, which is not easy for an European like myself, used to the day-month date format)

    As illustrated in "time.parse : why does golang parses the time incorrectly?", that layout (using 1,2,3,4,5,6,7) must be respected exactly.

提交回复
热议问题