Created
November 11, 2024 18:37
-
-
Save chrism/9bd77cb4225cdc519401acb5e099ce79 to your computer and use it in GitHub Desktop.
convert HMS
This file contains hidden or 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 | |
let a = []; | |
if (hours) { | |
a.push(`${hours} h`) | |
} | |
if (minutes) { | |
a.push(`${minutes} min`) | |
} | |
if (!hours && !minutes && seconds) { | |
a.push(`${seconds} s`) | |
} | |
return a.join(' '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment