Datetime
- Python date string to date object
If you have a date string and need to convert it into a date object, how can it be converted to a date object using Python?
Python includes within the datetime package a strptime function, here’s an example usage:
>>> import datetime >>> datetime.datetime.strptime('2022-10-30T14:32:41Z', "%Y-%m-%dT%H:%M:%S%z") datetime.datetime(2022, 10, 30, 14, 32, 41, tzinfo=datetime.timezone.utc)Updated code for Windows
Recently I ran into an issue where a report was generated on Linux but I wanted to process it further on windows. The timezones don’t match up though and will cause issues. Let’s look at how to handle that.