Skip to content

Instantly share code, notes, and snippets.

@ikedumancas
Created June 10, 2018 07:44
Show Gist options
  • Select an option

  • Save ikedumancas/a65172d7b9edd0caac9fbaf0983d7f96 to your computer and use it in GitHub Desktop.

Select an option

Save ikedumancas/a65172d7b9edd0caac9fbaf0983d7f96 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)
@ranzes
Copy link

ranzes commented Sep 15, 2024

Thank you so much!!!!

@oldmate45
Copy link

works a treat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment