How to Convert Seconds to Hours, Minutes and Seconds in Python
In this article, we will learn to convert seconds to hours, minutes, and seconds in Python. We will use some built-in modules available and some custom codes as well to see them working. Let's first have a quick look over what are dates in Python.
In Python, we can work on Date functions by importing a built-in module
datetime available in Python. We have date objects to work with dates. This
datetime module contains dates in the form of year, month, day, hour, minute, second, and microsecond. The datetime module has many methods to return information about the date object. It requires date, month, and year values to compute the function. Date and time functions are compared like mathematical expressions between various numbers.
Convert seconds to hours, minutes, and seconds
In Python, the date and time module provides various functions for the manipulation of dates. We can also convert seconds into hours, minutes, and seconds by applying mathematical operations. Let us discuss the various ways to perform the conversion.
Example: Using Simple Mathematical Calculations
It calculates seconds, hours, and minutes individually from the given seconds. Hour is calculated by floor division (
//) of seconds by 3600. Minutes are calculated by floor division of remaining seconds. Seconds are also calculated by the remainder of hour and minutes calculation. In the print statement, string formatting is done to print in the preferred format.
seconds = 12601 seconds = seconds % (24 * 3600) hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 print("%d:%02d:%02d" % (hour, minutes, seconds))
Example: Using divmod() function
The below example uses
divmod() function. This function performs a single division and results in the quotient and the remainder.
seconds = 31045 minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) print("%d:%02d:%02d" % (hours, minutes, seconds))
Example: Using datetime Module
datetime module of Python provides
datetime.timedelta() function to convert seconds into hours, minutes, and seconds. It takes seconds as an argument and prints the seconds in the preferred format.
timedelta(seconds) is called to convert seconds to a timedelta object and
str(object) is called with timedelta object to return a string representing
seconds as hours, minutes, and seconds.
import datetime sec = 9506 convert = str(datetime.timedelta(seconds = sec)) print(convert)
Example: Using the time module
The time module of Python provides
datetime.strftime() function to convert seconds into hours, minutes, and seconds. It takes time format and
time.gmtime() function as arguments.
strftime() - It prints the seconds in the preferred format.
gmtime() - It is used to convert seconds to the specified format that
import time seconds = 52910 convert = time.strftime("%H:%M:%S", time.gmtime(seconds)) print(convert)
In this article, we learned to convert seconds into hours, minutes, and seconds format by using
time module and two simple mathematical approaches. We used some custom codes as well to better understand the working of each method.