Skip to content

Seconds to HH:MM:SS

Format an integer number of seconds as a zero-padded hours, minutes, and seconds string.

18th August 2026

def seconds_to_hms(sec)
  [sec / 3600, sec / 60 % 60, sec % 60].map { |t| t.to_s.rjust(2, '0') }.join(':')
end