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 timedelta function.

For instance, we write

import datetime

d = str(datetime.timedelta(seconds=666))

to create a timedelta object by calling datetime.timedelta with the seconds argument set to the number of seconds.

Then we convert it to a string with str to get the timedelta in hours:minutes:seconds format.

Conclusion

To convert seconds to hours, minutes and seconds with Python, we can use the timedelta function.

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 *