Convert JSON date string to Python datetime

后端 未结 2 1980
走了就别回头了
走了就别回头了 2020-12-08 13:14

When translating dates to JSON, javascript is saving dates in this format:

2012-05-29T19:30:03.283Z

However, I am not sure how to get this

2条回答
  •  时光取名叫无心
    2020-12-08 14:00

    Try the following format:

    %Y-%m-%dT%H:%M:%S.%fZ
    

    For example:

    >>> datetime.datetime.strptime('2012-05-29T19:30:03.283Z', '%Y-%m-%dT%H:%M:%S.%fZ')
    datetime.datetime(2012, 5, 29, 19, 30, 3, 283000)
    

    The Z in the date just means that it should be interpreted as a UTC time, so ignoring it won't cause any loss of information. You can find this information here: http://www.w3.org/TR/NOTE-datetime

提交回复
热议问题