Created
November 4, 2017 15:25
-
-
Save bcls/48fad4f1fe9d400dc35a3d071c097be0 to your computer and use it in GitHub Desktop.
seconds to hh:mm:ss
This file contains 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
/** | |
* 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