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
The datetime
standard library introduced a function for inverting datetime.isoformat()
.
classmethod datetime.fromisoformat(date_string):
Return a
datetime
corresponding to adate_string
in one of the formats emitted bydate.isoformat()
anddatetime.isoformat()
.Specifically, this function supports strings in the format(s):
YYYY-MM-DD[*HH[:MM[:SS[.mmm[mmm]]]][+HH:MM[:SS[.ffffff]]]]
where
*
can match any single character.Caution: This does not support parsing arbitrary ISO 8601 strings - it is only intended as the inverse operation of
datetime.isoformat()
.
Example of use:
from datetime import datetime
date = datetime.fromisoformat('2017-01-01T12:30:59.000000')