Convert Seconds into Hours, Minutes, and Seconds in Python

In this Python program , we are going to learn how to convert Seconds time into Hours , Minutes and seconds with the help of timedelta.

Python program to convert seconds into hours and minutes

``````import datetime
sec = 9625
convertedHrMinSec = (datetime.timedelta(seconds = sec))
print("The converted Hours, Minutes and Seconds are =",convertedHrMinSec)``````

Output:

The converted Hours, Minutes and Seconds are = 2:40:25

Python program to convert minutes into hours and minutes

``````import datetime
min = 625
convertedHrMinSec = (datetime.timedelta(minutes = min))
print("The converted Hours, Minutes and Seconds are =",convertedHrMinSec)``````

Output:

The converted Hours, Minutes and Seconds are = 10:25:00

Source Code Explanation

1)The datetime module of Python provides datetime.timedelta() function to convert seconds into hours, minutes, and seconds.
2)It takes seconds as an argument and prints the seconds in the preferred format.