Home > Software design >  convert datetime to timestamp
convert datetime to timestamp

Time:12-03

First, what does this time format mean '2020-01-01T01:39:40.000000Z', my guess is that, it means timezone, I stand to be corrected. I want to convert it to Unix timestamp in python

This works fine

from datetime import datetime
datetime.fromisoformat('2011-11-04T00:05:23').timestamp()

output

1320365123.0

but this give me an error

from datetime import datetime
datetime.fromisoformat('2020-01-01T01:39:40.000000Z').timestamp()

error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-39-0489f93685df> in <module>
      3 # datetime.fromisoformat('2011-11-04T00:05:23').timestamp()
      4 
----> 5 datetime.fromisoformat('2020-01-01T01:39:40.000000Z').timestamp()

ValueError: Invalid isoformat string: '2020-01-01T01:39:40.000000Z'

my data is coming in this format, since I don't know what it means. I am not motivated to remove the 000000Z before converting it

CodePudding user response:

Python does not fully support the ISO 8601 standard. You should use dateutil.parser instead:

>>> from dateutil.parser import isoparse
>>> isoparse("2020-01-01T01:39:40.000000Z")
datetime.datetime(2020, 1, 1, 1, 39, 40, tzinfo=tzutc())
  • Related