How to convert timestamps with offset to datetime object using strptime with Python?

Sometimes, we want to convert timestamps with offset to datetime object using strptime with Python.

In this article, we’ll look at how to convert timestamps with offset to datetime object using strptime with Python.

How to convert timestamps with offset to datetime object using strptime with Python?

To convert timestamps with offset to datetime object using strptime with Python, we can call strptime with the substring that has the date and time without the offset.

For instance, we write

time_obj = datetime.datetime.strptime(time_str[:19], '%Y-%m-%dT%H:%M:%S')

to call strptime with time_str[:19] to parse the part of time_str without the time zone.

We use the '%Y-%m-%dT%H:%M:%S' format string to parse the date and time parts.

Conclusion

To convert timestamps with offset to datetime object using strptime with Python, we can call strptime with the substring that has the date and time without the offset.