Converting seconds to days, hours, minutes and seconds in Python

There are several ways to convert seconds to days, hours, minutes and seconds. Here we will see the easiest way to do that. Using the datetime module’s timedelta() object, you can convert the seconds in integer or decimal (with microseconds) value to days, hours, minutes and seconds in one line of code. Here is an example.

## Converting seconds to days, hours, minutes and seconds in Python
import datetime, time

secs = 123456789.1236

result = datetime.timedelta(seconds = secs)

print("\n", result, "\n")
Converting seconds to days, hours, minutes and seconds i Python

Reference

More Tips


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.