Parsing date string in Go

前端 未结 7 1956
小蘑菇
小蘑菇 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 16:00

    This is rather late to the party, and not really saying anything that hasn't been already said in one form or another, mostly through links above, but I wanted to give a TL;DR recap to those with less attention span:

    The date and time of the go format string is very important. It's how Go knows which field is which. They are generally 1-9 left to right as follows:

    • January / Jan / january / jan / 01 / _1 (etc) are for month
    • 02 / _2 are for day of month
    • 15 / 03 / _3 / PM / P / pm /p are for hour & meridian (3pm)
    • 04 / _4 are for minutes
    • 05 / _5 are for seconds
    • 2006 / 06 are for year
    • -0700 / 07:00 / MST are for timezone
    • .999999999 / .000000000 etc are for partial seconds (I think the distinction is if trailing zeros are removed)
    • Mon / Monday are day of the week (which 01-02-2006 actually was),

    So, Don't write "01-05-15" as your date format, unless you want "Month-Second-Hour"

    (... again, this was basically a summary of above.)

    0 讨论(0)
提交回复
热议问题