Categories
Python Answers

How to convert seconds to hours, minutes and seconds with Python?

Spread the love

Sometimes, we want to convert seconds to hours, minutes and seconds with Python.

In this article, we’ll look at how to convert seconds to hours, minutes and seconds with Python.

How to convert seconds to hours, minutes and seconds with Python?

To convert seconds to hours, minutes and seconds with Python, we can use the datetime module.

For instance, we write:

import datetime

t = str(datetime.timedelta(seconds=10000))
print(t)

We call datetime.timedelta with the seconds set.

Then we call str on the returned object to get the seconds in hours, minutes, and seconds format.

Therefore, t is '2:46:40'.

Conclusion

To convert seconds to hours, minutes and seconds with Python, we can use the datetime module.

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 *