Skip to content

Instantly share code, notes, and snippets.

@chrism
Created November 11, 2024 18:37
Show Gist options
  • Save chrism/9bd77cb4225cdc519401acb5e099ce79 to your computer and use it in GitHub Desktop.
Save chrism/9bd77cb4225cdc519401acb5e099ce79 to your computer and use it in GitHub Desktop.
convert HMS
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