Skip to content

Instantly share code, notes, and snippets.

@wafe
Forked from g1eb/convertTime.js
Created November 9, 2023 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wafe/ca2995cd199f3372e1c7034f19fbcf5c to your computer and use it in GitHub Desktop.
Save wafe/ca2995cd199f3372e1c7034f19fbcf5c 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