Created
February 15, 2022 06:47
-
-
Save niranyousuf/f24129fe9505aa63f862cbfb9acf2900 to your computer and use it in GitHub Desktop.
Convert seconds to minutes and hours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment