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")
- About datetime.timedelta() at Python docs.
- How to generate time from it’s individual components hours, minutes and seconds in Python?
- How to segregate the current time part from date time in Python?