How do I parse an ISO 8601-formatted date?

后端 未结 27 2427
小鲜肉
小鲜肉 2020-11-21 06:08

I need to parse RFC 3339 strings like \"2008-09-03T20:56:35.450686Z\" into Python\'s datetime type.

I have found strptime in the Python sta

27条回答
  •  眼角桃花
    2020-11-21 06:20

    Starting from Python 3.7, strptime supports colon delimiters in UTC offsets (source). So you can then use:

    import datetime
    datetime.datetime.strptime('2018-01-31T09:24:31.488670+00:00', '%Y-%m-%dT%H:%M:%S.%f%z')
    

    EDIT:

    As pointed out by Martijn, if you created the datetime object using isoformat(), you can simply use datetime.fromisoformat()

提交回复
热议问题