0

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

se7en
  • 671
  • 4
  • 18
  • Just remove that last `Z` from the ISO string and it will work. – accdias Dec 02 '21 at 12:02
  • Does this answer your question? [How can I convert the string '2020-01-06T00:00:00.000Z' to datetime in python?](https://stackoverflow.com/questions/59744589/how-can-i-convert-the-string-2020-01-06t000000-000z-to-datetime-in-python) – accdias Dec 02 '21 at 12:06
  • Just replace `Z` with `+00:00` before parsing. See also https://stackoverflow.com/a/62769371/10197418 – FObersteiner Dec 02 '21 at 12:19

1 Answers1

2

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())
Florin C.
  • 593
  • 4
  • 13
  • `isoparse("2020-01-01T01:39:40.000000Z").timestamp()` changes it to Unix timestamp. Thanks for this – se7en Dec 02 '21 at 13:56