Skip to content

Instantly share code, notes, and snippets.

@augustovictor
Created November 18, 2017 15:28
Show Gist options
  • Select an option

  • Save augustovictor/ddbe6e92b2cc07ae3fab89c3e3e58f12 to your computer and use it in GitHub Desktop.

Select an option

Save augustovictor/ddbe6e92b2cc07ae3fab89c3e3e58f12 to your computer and use it in GitHub Desktop.
/*
Helper functions to calculate difference in hours, minutes and seconds between two dates
*/
function diffMinutes(initial, final) {
if(initial > final) return 59 + final - initial
return final - initial
}
function getDiffTime(initialDate, finalDate) {
return {
hours : Math.floor((finalDate - initialDate) / (1000 * 60 * 60)),
minutes: diffMinutes(initialDate.getMinutes(), finalDate.getMinutes()),
seconds: Math.abs(finalDate.getSeconds() - initialDate.getSeconds())
}
}
// Testing
/*
var releaseDate = new Date('2017-11-21T23:59:59')
setInterval(function() {console.log(getDiffTime(new Date(), releaseDate))}, 1000)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment