Skip to content

Instantly share code, notes, and snippets.

@huozhi
Created July 11, 2017 09:43
Show Gist options
  • Save huozhi/2d0840ea90556ee040749db9229bd723 to your computer and use it in GitHub Desktop.
Save huozhi/2d0840ea90556ee040749db9229bd723 to your computer and use it in GitHub Desktop.
format seconds with given formatter
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