Skip to content

Instantly share code, notes, and snippets.

@shanebo
Last active April 11, 2020 09:42
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 shanebo/3412560d72114491339bfdddb298c8af to your computer and use it in GitHub Desktop.
Save shanebo/3412560d72114491339bfdddb298c8af to your computer and use it in GitHub Desktop.
Simple seconds to hh:mm:ss functions
// this has a max of 24 hrs
const timeLeft = (secs) =>
new Date(secs * 1000)
.toISOString()
.substr(11, 8)
.replace(/^([00:])*/g, '') || '0';
// this could be extended to include days
const timeLeft = (time) => {
const hrs = Math.floor(time / 3600);
const mins = Math.floor((time % 3600) / 60);
const secs = Math.floor(time % 60);
return [hrs, mins, secs]
.map(n => n < 10 ? `0${n}` : n.toString())
.join(':')
.replace(/^([00:])*/g, '') || '0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment