Last active
September 27, 2017 20:54
-
-
Save bcls/fc826e0178aa1ce8c7ae7345af45f4a5 to your computer and use it in GitHub Desktop.
seconds to formatted time #javascript
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