Skip to content

Instantly share code, notes, and snippets.

@russo97
Created April 11, 2023 23:10
Show Gist options
  • Save russo97/2e0b1a01cbc5e1b4c780fd475f9eeccc to your computer and use it in GitHub Desktop.
Save russo97/2e0b1a01cbc5e1b4c780fd475f9eeccc to your computer and use it in GitHub Desktop.
converting seconds to an object containing hours, minutes, and seconds, respectively.
const SECONDS_PER_HOUR = 3600;
const SECONDS_PER_MINUTE = 60;
function toFloor (value) {
return Math.floor(value);
}
function secondsToHours (secs) {
const hours = toFloor(secs / SECONDS_PER_HOUR);
const minutes = toFloor(secs % SECONDS_PER_HOUR / SECONDS_PER_MINUTE);
const seconds = secs % SECONDS_PER_MINUTE;
return {
h: hours,
m: minutes,
s: seconds
}
}
// usage examples
console.log(secondsToHours(108000)); // 30h 0m 0s
console.log(secondsToHours(694)); // 0h 11m 34s
console.log(secondsToHours(3599)); // 0h 59m 59s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment