Skip to content

Instantly share code, notes, and snippets.

@niranyousuf
Created February 15, 2022 06:47
Show Gist options
  • Save niranyousuf/f24129fe9505aa63f862cbfb9acf2900 to your computer and use it in GitHub Desktop.
Save niranyousuf/f24129fe9505aa63f862cbfb9acf2900 to your computer and use it in GitHub Desktop.
Convert seconds to minutes and hours
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