Created
August 29, 2013 18:52
-
-
Save agarzon/6381974 to your computer and use it in GitHub Desktop.
Calculate remaining time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var diffSeconds = Math.round(new Date("September 21, 2013 12:00:00 PM").getTime()/1000.0) - Math.round(new Date().getTime()/1000.0); | |
var days = Math.floor(diffSeconds / 86400); | |
var hours = Math.floor(diffSeconds / 3600) % 24; | |
var minutes = Math.floor(diffSeconds / 60) % 60; | |
var seconds = diffSeconds % 60; | |
function pad(number, length) { | |
var str = '' + number; | |
while (str.length < length) { | |
str = '0' + str; | |
} | |
return str; | |
} | |
if (diffSeconds > 0) { | |
var remainingTime = pad(days, 2) + ':' + pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2); | |
} else { | |
var remainingTime = '00:00:00:00'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment