Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to convert seconds to HH:MM:SS and convert HH:MM:SS back to seconds in Python

In Python, you can convert seconds to an HH:MM:SS formatted string and vice versa using the built-in divmod() function and some basic arithmetic.

  • Convert seconds to HH:MM:SS:
def seconds_to_hms(seconds):
    hours, remainder = divmod(seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)

# Example usage
total_seconds = 3665
formatted_time = seconds_to_hms(total_seconds)
print(formatted_time)  # Output: '01:01:05'

In this example, the seconds_to_hms() function takes a number of seconds as input and converts it to an HH:MM:SS formatted string. The divmod() function is used to divide the total seconds by 3600 to get the hours and the remainder, which is then divided by 60 to get the minutes and seconds.

  • Convert HH:MM:SS back to seconds:
def hms_to_seconds(hms):
    hours, minutes, seconds = map(int, hms.split(':'))
    return hours * 3600 + minutes * 60 + seconds

# Example usage
formatted_time = '01:01:05'
total_seconds = hms_to_seconds(formatted_time)
print(total_seconds)  # Output: 3665

In this example, the hms_to_seconds() function takes an HH:MM:SS formatted string as input and converts it back to the total number of seconds. The split() function is used to split the string into hours, minutes, and seconds, which are then converted to integers using the map() function. The total number of seconds is calculated by multiplying the hours and minutes by 3600 and 60, respectively, and adding them together with the seconds.

  1. Python convert seconds to HH:MM:SS:

    seconds = 3665  # Example seconds
    formatted_time = '{:02}:{:02}:{:02}'.format(seconds // 3600, (seconds % 3600) // 60, seconds % 60)
    print(formatted_time)
    
  2. Convert HH:MM:SS to seconds in Python:

    time_str = '01:01:05'  # Example time string
    h, m, s = map(int, time_str.split(':'))
    total_seconds = h * 3600 + m * 60 + s
    print(total_seconds)
    
  3. Convert seconds to timedelta in Python:

    from datetime import timedelta
    
    seconds = 3665  # Example seconds
    time_delta = timedelta(seconds=seconds)
    print(str(time_delta))
    
  4. Convert seconds to hours, minutes, and seconds in Python:

    seconds = 3665  # Example seconds
    hours, remainder = divmod(seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    formatted_time = '{:02}:{:02}:{:02}'.format(hours, minutes, seconds)
    print(formatted_time)
    
  5. Python strftime for converting seconds to time:

    from time import strftime, gmtime
    
    seconds = 3665  # Example seconds
    formatted_time = strftime('%H:%M:%S', gmtime(seconds))
    print(formatted_time)
    
  6. Python time.strptime for converting HH:MM:SS to seconds:

    from time import strptime
    
    time_str = '01:01:05'  # Example time string
    struct_time = strptime(time_str, '%H:%M:%S')
    total_seconds = struct_time.tm_hour * 3600 + struct_time.tm_min * 60 + struct_time.tm_sec
    print(total_seconds)
    
  7. Convert seconds to time object in Python:

    from datetime import datetime, timedelta
    
    seconds = 3665  # Example seconds
    time_object = (datetime.min + timedelta(seconds=seconds)).time()
    print(str(time_object))
    
  8. Format timedelta as HH:MM:SS in Python:

    from datetime import timedelta
    
    seconds = 3665  # Example seconds
    time_delta = timedelta(seconds=seconds)
    formatted_time = str(time_delta)
    print(formatted_time)
    
  9. Convert seconds to formatted time using format() in Python:

    seconds = 3665  # Example seconds
    formatted_time = '{:0>8}'.format(str(timedelta(seconds=seconds)))
    print(formatted_time)