Convert seconds to hh:mm:ss format
Format a whole number of seconds as zero-padded hours, minutes and seconds.
function formatSeconds(seconds) { return [Math.floor(seconds / 3600), Math.floor(seconds % 3600 / 60), seconds % 60].map(part => String(part).padStart(2, '0')).join(':'); }
Input & expected output
Input
3725Expected output
"01:02:05"The edge case
Input
90061Expected output
"25:01:01"Know the limits
- Hours keep counting past 24. The original toISOString version wrapped around after one day.
- Input is a non-negative integer number of seconds.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Convert seconds to hh:mm:ss format from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Replaced the toISOString variant, which wrapped after 24 hours, with zero-padded arithmetic that keeps counting hours. Original MDX retained as inert source evidence.
Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/convert-seconds-to-hh-mm-ss-format.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.
Keep exploring
- Compare two dates — Compare validated ISO calendar dates in UTC and return -1, 0 or 1.
- Add AM PM suffix to an hour — Turn an hour from 0 to 23 into a 12-hour label with am or pm.
- Calculate the number of days between two dates — Count whole calendar days between two YYYY-MM-DD dates.