0

Im trying to get from a set of ISO8601 timestampts (sort to say) a correct datetime object in Python 3.9, the problem comes here:

When trying to parse (for instance) the ISO8601 timestamp 2021-06-25T14:00:00.000Z into a datetime object un Python I get a wrong date and time, while if I do it in Javascript with the Date() object instance I get the correct one; here's in code how I did it:

Python:

from dateutil import parser

output = parser.parse('2021-06-25T14:00:00.000Z')

# output value is:
datetime.datetime(2021, 6, 29, 14, 0, tzinfo=tzutc())

# accessing the hour of that output
output.hour # 14 , it shouldn't be 14

Javascript

output = new Date('2021-06-25T14:00:00.000Z')

// calling output outputs:
Fri Jun 25 2021 09:00:00 GMT-0500 (hora de verano central) // as you can see, it says that is 9am which IS CORRECT 

So recap, the Javascript output is the correct and I want such output to be the same as with Python, Why is this happening? how to solve it?

NOTE:

I cannot use javascript obviously so I'm in complete need of doing it by Python 3.9

  • Of course 14 is the right answer. That is a datetime object in the UTC timezone, as it clearly states, and in the UTC timezone, the hour is 14. If you want to convert this to your local time zone, use `output.astimezone(pytz.timezone('US/Central'))`. – Tim Roberts Jun 26 '21 at 04:25
  • 1
    Does this answer your question? [How to convert Python's .isoformat() string back into datetime object](https://stackoverflow.com/questions/28331512/how-to-convert-pythons-isoformat-string-back-into-datetime-object) – riskRewarded Jun 26 '21 at 04:26

0 Answers0