I have a datetime.datetime object that gets stored as text in a text file. Later, I have to convert that text back to a datetime.datetime object. The object is in this format 2022-04-18T10:00:00-07:00
but I'm having trouble using the strp time function to convert the text back. I tried running date_time_obj = datetime.strptime(timedatestamp, '%y-%m-%dT%H:%M:%S%z')
but that format doesn't work because the time data is formatted wrong. Any clue as to what I'm doing wrong?
Asked
Active
Viewed 123 times
2

FObersteiner
- 22,500
- 8
- 42
- 72

Duaa Zaheer
- 91
- 5
-
It's `%Y` for a four-digit year, not %y. Or use datetime 's `fromisoformat`. – FObersteiner Apr 26 '22 at 06:22
-
Does this answer your question? [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – FObersteiner Apr 26 '22 at 06:23