How to Convert Seconds to hh:mm:ss in Python?

Posted in

How to Convert Seconds to hh:mm:ss in Python?
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    While Programming or writing script, we come across the situation where we need to convert the given seconds into readable hours:minutes: seconds format or vice versa.

    This Python tutorial will teach you how to convert a given integer seconds value to hours, minutes, and seconds. And by the end of this tutorial, you will be able to write Python programs and logic for

    1. Conversion of seconds into hours, minutes, and seconds
    2. Conversion of Hours, minutes, and seconds into seconds.

    So let’s Get started.

    How to convert seconds to Hours, Minutes, and Seconds in Python with timedelta class?

    To handle time and date data in Python, we have the inbuilt datetime module. The datetime module contains various subclasses and methods to manipulate, represent, and edit time in Python. In the datetime module, we also the timedelta class that can convert given integer seconds to readable hh:mm:ss format. To use timedelta in Python, follow the following steps

    Step 1: Import the timedelta class from the datetime module

    The datetime is an inbuilt Python module, to import it in our Python script, we can use the from and import keywords.

    from datetime import  timedelta

    Step 2: Convert the given seconds into hh:mm:ss using timedelta

    The timedelta() class can accept the given seconds as keyword argument seconds , and return a datetime.timedelta object representing the given seconds into hh:mm:ss format.

    #seconds
    given_seconds = 6000
    
    print("Time in Seconds is: ",given_seconds)
    
    #convert the seconds into hh:mm:ss
    td = timedelta(seconds = given_seconds)
    
    print(f"The {given_seconds} are equal to hh:mm:ss=", td)

    Output

    Time in Seconds is:  6000
    The 6000 are equal to hh:mm:ss= 1:40:00

    Example 2

    For the given seconds argument, the timedelta() class returns timedelta() object in hh:mm:ss format, which is not that readable.  To display the conversion of seconds into hours, minutes, and seconds, we first have to convert the return result into a string using str() function, then display it correctly.

    from datetime import timedelta
    
    #seconds
    given_seconds1 = 5463
    
    print("Time in Seconds is: ",given_seconds1)
    
    #convert the seconds into hh:mm:ss string
    td1 = str(timedelta(seconds = given_seconds1))
    
    #get the hh, mm and ss from the string
    hh1, mm1, ss1 = td1.split(":") 
    
    print(f"The {given_seconds1} seconds in hh:mm:ss is {hh1} Hours {mm1} Minutes {ss1} Seconds \n" )
    
    #seconds
    given_seconds2 = 78374
    
    print("Time in Seconds is: ",given_seconds2)
    
    #convert the seconds into hh:mm:ss string
    td1 = str(timedelta(seconds = given_seconds2))
    
    #get the hh, mm and ss from the string
    hh1, mm1, ss1 = td1.split(":") 
    
    print(f"The {given_seconds2} seconds in hh:mm:ss is {hh1} Hours {mm1} Minutes {ss1} Seconds \n" )

    Output

    Time in Seconds is:  5463
    The 5463 seconds in hh:mm:ss is 1 Hours 31 Minutes 03 Seconds 
    
    Time in Seconds is:  78374
    The 78374 seconds in hh:mm:ss is 21 Hours 46 Minutes 14 Seconds

    How to convert seconds to Hours, Minutes, and Seconds in Python with the time module?

    Python also has a time module that is specifically used to deal with time data in Python.  The time module supports a gmtime() method that is used to convert given seconds to a full date and time from the epoch that is January 1, 1970, 00:00:00 (UTC). We can take the help of this method and convert the given seconds into hours, minutes, and seconds.

    Example

    import time
    
    #seconds
    given_seconds = 86399
    
    print("Time in Seconds is: ",given_seconds)
    
    #get the time
    td = time.gmtime(given_seconds)
    
    #convert the gmtime object to string
    td = time.strftime("%H Hours %M Minutes %S seconds", td)
    
    print(f"The {given_seconds} seconds in hh:mm:ss {td}" )

    Output

    Time in Seconds is:  86399
    The 86399 seconds in hh:mm:ss 23 Hours 59 Minutes 59 seconds
    Note: The downside of time.gmtime() method, it can only convert given seconds upto 23 hours, 59 minutes, and 59 seconds. If the seconds are more than  86399 it will show some inappropriate results.

    How to convert seconds to Hours, Minutes, and Seconds in Python with Naive logic?

    You are giving a Python interview. The interviewer asks you to write a Python script that can convert given seconds into hours, minutes, and seconds without using any inbuilt module or function. In such a situation, you need to write a logic that can convert the seconds to the required output. This hardcoded logic technique is also known as naive logic or naive method.

    Seconds to the hour, minutes, and second conversion Logic

    seconds = give_seconds % (24*3600)  #here 24 represents the hours for 1 day only
    
    hours =  seconds  / 3600
    
    seconds = seconds % 3600
    
    minutes = seconds / 60 
    
    seconds = seconds % 60

    Example

    #seconds
    given_seconds = 86400
    
    print("Time in Seconds is: ",given_seconds)
    
    #8760 represent the hours in a year
    seconds = given_seconds % (8760*3600)
    
    hours = seconds // 3600
    
    seconds = seconds % 3600
    
    minutes = seconds //60
    
    seconds = seconds % 60
    
    print(f"The {given_seconds} seconds in hh:mm:ss is {hours} Hours {minutes} Minutes {seconds} Seconds" )

    Output

    Time in Seconds is:  86400
    The 86400 seconds in hh:mm:ss is 24 Hours 0 Minutes 0 Seconds

    How to Convert Hours, Minutes, and Seconds to seconds in Python

    The conversion of hours, minutes, and seconds into total seconds is effortless, all we do is convert every individual entity to seconds and sum them. For example, we can convert the hour into seconds by multiplying the hour value by 3600 because there are 3600 seconds in an hour. Similarly, we can multiply the given minutes by 60 seconds by 1 and sum them together.

    Formula to convert hours, minutes, and seconds to seconds

    (hours * 3600) + (minutes * 60) + (seconds*1)

    Example

    def total_seconds(hh_mm_ss):
        #split the string into hour, minutes and seconds
        hour , minutes , seconds = hh_mm_ss.split(":")
    
        return int(hour)*3600 + int(minutes)*60 + int(seconds)*1
    
    hh_mm_ss_1 = "01:00:00"
    hh_mm_ss_2 = "00:35:12"
    hh_mm_ss_3 = "00:55:15"
    
    print("Time in hh:mm:ss", hh_mm_ss_1)
    print("Time in Seconds: ", total_seconds(hh_mm_ss_1), end ="\n\n")
    
    print("Time in hh:mm:ss", hh_mm_ss_2)
    print("Time in Seconds: ", total_seconds(hh_mm_ss_2), end ="\n\n")
    
    print("Time in hh:mm:ss", hh_mm_ss_3)
    print("Time in Seconds: ", total_seconds(hh_mm_ss_3), end ="\n\n")

    Output

    Time in hh:mm:ss 01:00:00
    Time in Seconds:  3600
    
    Time in hh:mm:ss 00:35:12
    Time in Seconds:  2112
    
    Time in hh:mm:ss 00:55:15
    Time in Seconds:  3315

    Conclusion

    In this Python tutorial, you learned how to convert the given seconds into hours, minutes, and seconds. This tutorial discussed three different techniques you can use to perform the task, including using inbuilt modules and using a naive approach.

    In this tutorial, we have also discussed how we can convert the given hours, minutes, and seconds back to total seconds, which is very easy compared to converting the seconds to hh:mm:ss.

    If you have any suggestions regarding this article or any queries, please comment in the comments section. We will be appreciated your feedback.

    People are also reading:

    Leave a Comment on this Post

    0 Comments