How do I parse an ISO 8601-formatted date?

后端 未结 27 2315
小鲜肉
小鲜肉 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:19

    An another way is to use specialized parser for ISO-8601 is to use isoparse function of dateutil parser:

    from dateutil import parser
    
    date = parser.isoparse("2008-09-03T20:56:35.450686+01:00")
    print(date)
    

    Output:

    2008-09-03 20:56:35.450686+01:00
    

    This function is also mentioned in the documentation for the standard Python function datetime.fromisoformat:

    A more full-featured ISO 8601 parser, dateutil.parser.isoparse is available in the third-party package dateutil.

提交回复
热议问题