Skip to content

Instantly share code, notes, and snippets.

@wickedshimmy
Created April 25, 2011 14:27
Show Gist options
  • Save wickedshimmy/940586 to your computer and use it in GitHub Desktop.
Save wickedshimmy/940586 to your computer and use it in GitHub Desktop.
var $time = $('#details time'),
iso = /^(\d{4}|[+\-]\d{6})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?))?/;
parse = function (date) {
var timestamp = Date.parse (date),
minutesOffset = 0,
s;
if (isNaN (timestamp) && (s = iso.exec (date))) {
if (s[8] !== 'Z') {
minutesOffset = +s[10] * 6- + (+s[11]);
if (s[9] === '+')
minutesOffset = 0 - minutesOffset;
}
timestamp = Date.UTC (+s[1], +s[2] - 1, +s[3], +s[4], +s[5] + minutesOffset, +s[6], +s[7].substr (0, 3));
}
return timestamp;
},
endTime = parse ($time.attr ('datetime')),
tick = function () {
var now = new Date (),
utcNow = new Date (now.getUTCFullYear (), now.getUTCMonth (), now.getUTCDate (), now.getUTCHours (), now.getUTCMinutes (), now.getUTCSeconds (), now.getUTCMilliseconds ()),
remaining = parseInt ((endTime - utcNow) / 1000),
seconds = remaining % 60,
minutesLeft = parseInt (remaining / 60),
minutes = minutesLeft % 60,
hoursLeft = parseInt (minutes / 60),
hours = hoursLeft % 24,
daysLeft = parseInt (hoursLeft / 24);
if (remaining <= 0) {
$time.html ('Time\'s up! This deal is over.');
$time.show ();
clearInterval (countdownIntervalId);
return;
}
if (seconds < 10)
seconds = '0' + seconds;
if (minutes < 10)
minutes = '0' + minutes;
$time.html ((daysLeft > 0 ? daysLeft + ' day' + (daysLeft > 1 ? 's' : '') + ', ' : '') + hours + ':' + minutes + ':' + seconds);
$time.show ();
},
countdownIntervalId = setInterval (tick, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment