Menu Close

How to convert a unix timestamp string to readable date with Python?

Sometimes, we want to convert a unix timestamp string to readable date with Python.

In this article, we’ll look at how to convert a unix timestamp string to readable date with Python.

How to convert a unix timestamp string to readable date with Python?

To convert a unix timestamp string to readable date with Python, we can use the datetime.utcfromtimestamp method.

For instance, we write

from datetime import datetime
ts = 1284101485


print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

to call datetime.utcfromtimestamp with the ts timestanmp, whgich is in seconds.

to convert ts to a datetime object.

Conclusion

To convert a unix timestamp string to readable date with Python, we can use the datetime.utcfromtimestamp method.

Posted in Python, Python Answers