The datetime
standard library has, since Python 3.7, 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()
.Examples:
>>> from datetime import datetime >>> datetime.fromisoformat('2011-11-04') datetime.datetime(2011, 11, 4, 0, 0)
…
Be sure to read the caution from the docs!