Skip to content

Instantly share code, notes, and snippets.

@g1eb
Created September 23, 2017 09:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save g1eb/62d9a48164fe7336fdf4845e22ae3d2c to your computer and use it in GitHub Desktop.
Save g1eb/62d9a48164fe7336fdf4845e22ae3d2c to your computer and use it in GitHub Desktop.
Convert seconds to a human readable representation
export function convertTime(seconds) {
var seconds = parseInt(seconds, 10)
var hours = Math.floor(seconds / 3600)
var minutes = Math.floor((seconds - (hours * 3600)) / 60)
var seconds = seconds - (hours * 3600) - (minutes * 60)
if ( !!hours ) {
if ( !!minutes ) {
return `${hours}h ${minutes}m ${seconds}s`
} else {
return `${hours}h ${seconds}s`
}
}
if ( !!minutes ) {
return `${minutes}m ${seconds}s`
}
return `${seconds}s`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment