Changing Time from Epoch Time to Iso Format in Python [Duplicate]

Changing Time from Epoch Time to Iso Format in Python [Duplicate]

I would like to change the time from epoch time to a format readable by Kml extensions (such as iso format of time).

There is plenty of help to change from epoch to formats like YYYYMMDDHHMMSS and other structs using tuples and mktime, but to .iso formation , i haven't been able to find it.

2

1 Answer

utcfromtimestamp converts seconds since the epoch to the corresponding UTC datetime.datetime.

datetime.datetime objects have a isoformat method which returns the date as a string in ISO 8601 format.

In [6]: import datetime as DT

In [7]: seconds_since_epoch = 0

In [8]: DT.datetime.utcfromtimestamp(seconds_since_epoch)
Out[8]: datetime.datetime(1970, 1, 1, 0, 0)

In [9]: DT.datetime.utcfromtimestamp(seconds_since_epoch).isoformat()
Out[9]: '1970-01-01T00:00:00'
1
Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.