Created
July 11, 2017 09:43
-
-
Save huozhi/2d0840ea90556ee040749db9229bd723 to your computer and use it in GitHub Desktop.
format seconds with given formatter
This file contains hidden or 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
function formatTime(seconds, format) { | |
var values = { | |
h: Math.floor(seconds / 3600), | |
m: Math.floor(seconds % 3600 / 60), | |
s: Math.round(seconds % 60), | |
} | |
var type | |
var value | |
return format.split(':') | |
.map(formatter => { | |
type = formatter[0] | |
value = values[type] + '' | |
value = value.padStart(formatter.length - value.length, '0') | |
return value | |
}) | |
.join(':') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment