Skip to content

Seconds to a Digital Clock

A timer, a video player or a quiz often counts time as a plain number of seconds, but people expect to read it as a clock, such as 01:02:05. The conversion takes three steps: split the total into hours, minutes and seconds with division and the remainder operator, pad each part to two digits, and join the parts with colons.

An hour is 3,600 seconds, so dividing by 3,600 and rounding down with Math.floor gives the whole hours. The remainder operator % gives what is left over, so totalSeconds % 3600 is the seconds past the hour, and dividing that by 60 gives the minutes. totalSeconds % 60 is the seconds past the minute. padStart(2, '0') then adds a leading zero to any part with one digit.

function toClock(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return [hours, minutes, seconds]
.map((part) => String(part).padStart(2, '0'))
.join(':');
}
console.log(toClock(0));
console.log(toClock(75));
console.log(toClock(3725));
console.log(toClock(45296));

This prints:

00:00:00
00:01:15
01:02:05
12:34:56

The function expects a whole number of seconds that is zero or more. For a value such as 92.7, round it first with Math.floor, or the seconds part will keep its decimals. Hours are not wrapped at 24, so a long duration shows as 27:46:40 rather than starting again. Padding Methods has more on padStart.

setInterval calls a function repeatedly, here once a second. The callback shows the time left, takes one second off, and calls clearInterval when it reaches zero, so the timer stops and the program can finish.

let remaining = 3;
const timer = setInterval(() => {
console.log(toClock(remaining));
remaining -= 1;
if (remaining < 0) {
clearInterval(timer);
console.log('Done');
}
}, 1000);

This prints, one line a second:

00:00:03
00:00:02
00:00:01
00:00:00
Done

The example uses the toClock function from the previous section. An interval is not exact, because each callback can run a few milliseconds late and the delays add up over a long countdown. For a timer that must stay accurate, record the end time with Date.now() when it starts, and on each tick work out the seconds left from the difference, rather than subtracting one each time. Timers covers setInterval and setTimeout in detail.

A Date created from the number of milliseconds, read back with toISOString, already contains the time as hh:mm:ss, so slicing out those characters gives a clock in one line. It works only for durations under 24 hours, because the hours then roll over into the next day.

console.log(new Date(3725 * 1000).toISOString().slice(11, 19));
console.log(new Date(90000 * 1000).toISOString().slice(11, 19));

This prints:

01:02:05
01:00:00

The second line should be 25 hours, but it shows 01:00:00.

Intl.DurationFormat, added in ES2025 and available in current browsers and recent versions of Node, formats a duration for a locale. Its digital style gives a clock-like result, although it does not pad the hours to two digits.

const format = new Intl.DurationFormat('en-GB', { style: 'digital', hoursDisplay: 'always' });
console.log(format.format({ hours: 1, minutes: 2, seconds: 5 }));
console.log(format.format({ hours: 0, minutes: 1, seconds: 15 }));

This prints:

1:02:05
0:01:15

It takes the duration already split into parts, so the division step is still needed.

Steve Griffith covers this topic in his video Displaying Seconds like a Digital Clock with JavaScript.