Skip to content

Instantly share code, notes, and snippets.

@bcls
Created November 4, 2017 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcls/48fad4f1fe9d400dc35a3d071c097be0 to your computer and use it in GitHub Desktop.
Save bcls/48fad4f1fe9d400dc35a3d071c097be0 to your computer and use it in GitHub Desktop.
seconds to hh:mm:ss
/**
* utility to extract h/m/s from seconds
* @param {number} secs - seconds to convert to hh:mm:ss
* @returns {object} object with members h (hours), m (minutes), s (seconds)
*/
function secondsToTime(secs) {
var hours = Math.floor(secs / (60 * 60)),
divisor_for_minutes = secs % (60 * 60),
minutes = Math.floor(divisor_for_minutes / 60),
divisor_for_seconds = divisor_for_minutes % 60,
seconds = Math.ceil(divisor_for_seconds),
obj = {};
if (hours < 10) {
hours = "0" + hours.toString();
} else {
hours = hours.toString();
}
if (minutes < 10) {
minutes = "0" + minutes.toString();
} else {
minutes = minutes.toString();
}
if (seconds < 10) {
seconds = "0" + seconds.toString();
} else {
seconds = seconds.toString();
}
obj = {
'h': hours,
'm': minutes,
's': seconds
};
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment