JSFiddle - React, Tailwind, and code Playground
by Sinh Nguyen
JavaScript
function secondsToHHMMSS(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
const seconds = totalSeconds - (hours * 3600) - (minutes * 60);
let hourStr, minuteStr, secondsStr;
// Padding the values to ensure they are two digits
if (hours < 10) { hourStr = "0" + hours; }
if (minutes < 10) { minuteStr = "0" + minutes; }
if (seconds < 10) { secondsStr = "0" + seconds; }
return hourStr + ':' + minuteStr + ':' + secondsStr;
}
// Example usage:
var totalSeconds = 3665;
console.log(secondsToHHMMSS(totalSeconds));