xxxxxxxxxx
minutes = (700 - (700%60))/60; //11
seconds = 700%60); //40
//11:40
xxxxxxxxxx
function convertHMS(value) {
const sec = parseInt(value, 10); // convert value to number if it's string
let hours = Math.floor(sec / 3600); // get hours
let minutes = Math.floor((sec - (hours * 3600)) / 60); // get minutes
let seconds = sec - (hours * 3600) - (minutes * 60); // get seconds
// add 0 if value < 10; Example: 2 => 02
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds; // Return is HH : MM : SS
}
xxxxxxxxxx
function getTime(time) {
//1:43
// console.log(Math.floor(time % 60))
return Math.floor(time / 60) + ':' + ('0' + Math.floor(time % 60)).slice(-2)
}
xxxxxxxxxx
function convertHMS(value) {
const sec = parseInt(value, 10); // convert value to number if it's string
let hours = Math.floor(sec / 3600); // get hours
let minutes = Math.floor((sec - (hours * 3600)) / 60); // get minutes
let seconds = sec - (hours * 3600) - (minutes * 60); // get seconds
// add 0 if value < 10; Example: 2 => 02
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds; // Return is HH : MM : SS
}
xxxxxxxxxx
function convertSeconds(seconds) {
var convert = function(x) { return (x < 10) ? "0"+x : x; }
return convert(parseInt(seconds / (60*60))) + ":" +
convert(parseInt(seconds / 60 % 60)) + ":" +
convert(seconds % 60)
}
xxxxxxxxxx
//this will converter second into minutes and seconds
function secondsToMinutesSeconds(seconds) {
var minutes = Math.floor(seconds / 60);
var remainingSeconds = seconds % 60;
return `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
}
xxxxxxxxxx
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);
xxxxxxxxxx
function convertHMS(value) {
const sec = parseInt(value, 10); // convert value to number if it's string
let hours = Math.floor(sec / 3600); // get hours
let minutes = Math.floor((sec - (hours * 3600)) / 60); // get minutes
let seconds = sec - (hours * 3600) - (minutes * 60); // get seconds
// add 0 if value < 10; Example: 2 => 02
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds; // Return is HH : MM : SS
}
xxxxxxxxxx
<html>
<head>
</head>
<body>
<h2>Convert seconds to minutes and seconds in JavaScript.</h2>
<h4>Using the <i>Math.floor()</i> method to convert the different values of seconds to minutes and seconds.</h4>
<p id = "output"></p>
<script>
let output = document.getElementById("output");
function convertStoMs(seconds) {
let minutes = Math.floor(seconds / 60);
let extraSeconds = seconds % 60;
minutes = minutes < 10 ? "0" + minutes : minutes;
extraSeconds = extraSeconds< 10 ? "0" + extraSeconds : extraSeconds;
output.innerHTML += seconds + " == " + minutes + " : " + extraSeconds + "<br/>";
}
convertStoMs(159);
convertStoMs(234567);
convertStoMs(9);
</script>
</body>
</html>