Format Integer to time in Format HH:MM:SS

Hello,
I try to format the following code to HH:MM:SS, but it gives me 1:24:9.
How can I format it to HH:MM:SS?

{% set duration = 5049 %}

{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %} 
{{ [hours, minutes, seconds]|join(':') }}

Thank you

Like this:

{% set duration = 5049 %}

{% set seconds = duration % 60 %}
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %} 

{{ '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds) }}

Or simply like this (using floor division):

{% set duration = 5049 %}

{{'{:02d}:{:02d}:{:02d}'.format(duration // 3600, (duration % 3600) // 60, (duration % 3600) % 60)}}

{{ 60 | timestamp_custom("%H:%M:%S", 0) }}

Replace 60 with seconds

3 Likes