There are cases where the application gets time information in seconds, but we need to store that information in hours, minutes, seconds (hh:mm:ss
) format, or vice-versa.
By the end of this article, you’ll learn: –
- How to convert seconds into hours, minutes, seconds (
hh:mm:ss
) format - How to convert hours, minutes, seconds (
hh:mm:ss
) to seconds.
How to Convert Seconds To Hours, Minutes, and Seconds using Timedelta
The below steps show how to convert seconds to hh:mm:ss
format in Python using the timedelta class.
- Import the datetime module
Python’s datetime module provides functions that handle many complex functionalities involving the date and time. Import it using a
import datetime
statement. - Use the timedelta class of a datetime module
The A timedelta represents a duration in days, hours, minutes, and seconds. Use the
timedelta()
constructor and pass the seconds value to it using theseconds
argument.
The timedelta constructor creates the timedelta object, representing time in days, hours, minutes, and seconds (days, hh:mm:ss.ms
) format.
For example,datetime.timedelta(seconds=6010)
will return 1 hour 40 minutes 10 seconds.
Example 1: Convert seconds to hh:mm:ss
Output:
Time in Seconds: 6010 Time in hh:mm:ss: 1:40:10
Example 2: Display it in Human readable format
- First, Create the timedelta object by passing seconds to it
- Next, convert the timedelta object to a string
- Next, split the string into individual components to get hours, minutes, and seconds
Output:
Time in seconds: 29500 Time in hh:mm:ss: 8 Hours 11 Minutes 40 Seconds Time in seconds: 7500040 Time in hh:mm:ss: 86 days, 19 Hours 20 Minutes 40 Seconds
Use Python built-in function divmod()
function if you don’t want to include days:
The divmod()
function Take two numbers as arguments and return a pair of numbers consisting of their quotient and remainder.
Output:
Time in Seconds: 7500040 Time in Seconds: 2083 Hours 20 Minutes 40 Seconds
Convert hh:mm:ss to Seconds
Now, let’s assume we have to inverse the above example, i.e., we want to convert hours, minutes, and seconds time string to seconds in Python.
Let’s assume you have a string in the format H:MM:SS
, and you need the represent it in seconds.
For example:
- “1:40:10” would produce an output of 6010 seconds
- “0:05:15” would produce an output of 315 seconds
- “0:00:32” would produce an output of 32 seconds
Steps to convert hours, minutes, seconds to seconds:
- Split the string into individual components and store it in the hours, minutes, and seconds variable.
- Multiply hours by 3600
- Multiple minutes by 60
- Add all the numbers to the seconds variable.
Example:
Output:
Time in hh:mm:ss: 1:40:10 Time in Seconds: 6010 Time in hh:mm:ss: 0:05:15 Time in Seconds: 315 Time in hh:mm:ss: 0:00:32 Time in Seconds: 32
Convert Current Time in Seconds to Days, hh:mm:ss
- Get the current time in seconds using the timestamp() function
- Pass it to the
timedelta()
constructor
Output:
Time in seconds: 1626698950.164824 Time in Days, hh:mm:ss.ms: 18827 days, 12:49:10.164824