Skip to content

Instantly share code, notes, and snippets.

@oldmate45
Forked from ikedumancas/utils.py
Created August 25, 2025 01:57
Show Gist options
  • Select an option

  • Save oldmate45/93686d6c14f9ebd83cc24777474be47a to your computer and use it in GitHub Desktop.

Select an option

Save oldmate45/93686d6c14f9ebd83cc24777474be47a to your computer and use it in GitHub Desktop.
Python: Convert timedelta to HHMMSS string
def format_timedelta_to_HHMMSS(td):
td_in_seconds = td.total_seconds()
hours, remainder = divmod(td_in_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
hours = int(hours)
minutes = int(minutes)
seconds = int(seconds)
if minutes < 10:
minutes = "0{}".format(minutes)
if seconds < 10:
seconds = "0{}".format(seconds)
return "{}:{}:{}".format(hours, minutes,seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment