Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *