Given an integer n (in seconds), convert it into hours, minutes and seconds.
Input : 12345 Output : 3:25:45 Input : 3600 Output : 1:00:00
Approach #1 : Naive
This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations.
Approach #2 : Alternate to the Naive approach
By using the
divmod() function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations.
Approach #3 : Using
timedelta (Object of
Datetime module provides timedelta object which represents a duration, the difference between two dates or times.
datetime.timedelta can be used to represent seconds into hours, minutes and seconds format.
Approach #4 : Using
time.strftime() gives more control over formatting. The format and
time.gmtime() is passed as argument. gmtime is used to convert seconds to special tuple format that
- Converting seconds into days, hours, minutes and seconds
- Program for converting hours into minutes and seconds
- Python | Pandas TimedeltaIndex.seconds
- Python | Pandas Timedelta.seconds
- Python program to convert POS to SOP
- Python program to convert hex string to decimal
- Python program to convert floating to binary
- Python | Program to convert String to a List
- Python | Program to convert a tuple to a string
- Python program to convert a list to string
- Python | Difference between two dates (in minutes) using datetime.timedelta() method
- Python program to convert decimal to binary number
- Python program to convert float decimal to Octal number
- Program to find the time after K minutes from given time
- Python program to convert camel case string to snake case
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.