Given a datetime.timedelta
, how to convert it into an ISO 8601 duration string?
For example, given datetime.timedelta(0, 18, 179651)
, how do I get 'PT18.179651S'
?
For the reverse, see Is there an easy way to convert ISO 8601 duration to timedelta?.
Although the
datetime
module contains an implementation for a ISO 8601 notation fordatetime
ordate
objects, it does not currently (Python 3.7) support the same fortimedelta
objects. However, theisodate
module (pypi link) has functionality to generate a duration string in ISO 8601 notation:which means 12148 days, 4 hours, 20 minutes, 39.47017 seconds.