Parsing date string in Go

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

    If you have worked with time/date formatting/parsing in other languages you might have noticed that the other languages use special placeholders for time/date formatting. For eg ruby language uses

    %d for day
    %Y for year
    

    etc. Golang, instead of using codes such as above, uses date and time format placeholders that look like date and time only. Go uses standard time, which is:

    Mon Jan 2 15:04:05 MST 2006  (MST is GMT-0700)
    or 
    01/02 03:04:05PM '06 -0700
    

    So if you notice Go uses

    01 for the day of the month,
    02 for the month
    03 for hours,
    04 for minutes
    05 for second
    and so on
    

    Therefore for example for parsing 2020-01-29, layout string should be 06-01-02 or 2006-01-02.

    You can refer to the full placeholder layout table at this link - https://golangbyexample.com/parse-time-in-golang/

提交回复
热议问题