javascript convert seconds to hours minutes seconds code example
Example 1: javascript convert seconds to minutes seconds
function convertHMS(value) {
const sec = parseInt(value, 10);
let hours = Math.floor(sec / 3600);
let minutes = Math.floor((sec - (hours * 3600)) / 60);
let seconds = sec - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
Example 2: convert seconds to hours minutes seconds javascript
function secondsToHms(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return hDisplay + mDisplay + sDisplay;
}
Example 3: how to convert seaconds into hh:mm:ss in javascript
var date = new Date(0);
date.setSeconds(45);
var timeString = date.toISOString().substr(11, 8);
console.log(timeString)
Example 4: convert seconds to minutes javascript
function secondsToHms(value) {
const sec = parseInt(value, 10);
let hours = Math.floor(sec / 3600);
let minutes = Math.floor((sec - hours * 3600) / 60);
let seconds = sec - hours * 3600 - minutes * 60;
if (hours < 10) { hours = '0' + hours; }
if (minutes < 10) { minutes = '0' + minutes; }
if (seconds < 10) { seconds = '0' + seconds; }
if (hours == 0) {
return +minutes + ':' + seconds;
} else {
return hours + ':' + minutes + ':' + seconds;
}
}
Example 5: javascript minute and second to convert seconds
function str_pad_left(string,pad,length) {
return (new Array(length+1).join(pad)+string).slice(-length);
}
var finalTime = str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);