I am trying to compare ISO DateTime with string datetime in Python code. I am getting datetime in JSON response from Azure Cloud which I need to compare to the last 24 hours before datetime. I am stuck at string-to-datetime conversion.
Here is the code
from datetime import datetime, timedelta
target_date = datetime.now() - timedelta (hours=24)
print(target_date.isoformat())
print(type(target_date))
createdOn = "2022-10-25T21:41:27.2824196Z"
post_time = datetime.strptime(createdOn, "%Y-%m-%dT%H:%M:%S.%f%z")
print(post_time)
I need to compare/check if createdOn is greater than target_date. While running the code, I am getting below error
2023-08-07T13:22:11.914140
<class 'datetime.datetime'>
Traceback (most recent call last):
File "/Users/Documents/GitHub/Mylabs/mytest.py", line 10, in <module>
post_time = datetime.strptime(createdOn, "%Y-%m-%dT%H:%M:%S.%f%z")
File "/Users/.pyenv/versions/3.8.17/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/Users/.pyenv/versions/3.8.17/lib/python3.8/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '2022-10-25T21:41:27.2824196Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'