Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting string formatted ISO date to Epoch in Python

I have a string formatted date 2021-12-14T12:05:51.8031499
How can I get the Epoch value of this?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dateutil is your friend, also with 7 digits of fractional seconds:

from dateutil.parser import isoparse

isoparse("2021-12-14T12:05:51.8031499")
Out[2]: datetime.datetime(2021, 12, 14, 12, 5, 51, 803149)

isoparse("2021-12-14T12:05:51.8031499").timestamp()
Out[3]: 1639479951.803149

Note: given ISO format date/time will result in a naive datetime object, which Python will treat as local time, i.e. it will be converted from your machine’s local time setting to UTC before Unix time is calculated for timestamp() !

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading