Skip to content

Instantly share code, notes, and snippets.

@jbenet
Created July 16, 2012 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbenet/3123711 to your computer and use it in GitHub Desktop.
Save jbenet/3123711 to your computer and use it in GitHub Desktop.

countdown!

Quick js countdown! Just link people to

http://bl.ocks.org/d/3123711/#<utc unix timestamp>

And voila!

/*
countdown.js v2.2.3 http://countdownjs.org
Copyright (c)2006-2012 Stephen M. McKamey.
Licensed under The MIT License.
*/
var module,countdown=function(l){function m(a,b){var c=a.getTime();a.setUTCMonth(a.getUTCMonth()+b);return o((a.getTime()-c)/864E5)}function g(a,b,c){return a+" "+(1===a?b:c)}function i(){}function n(a,b,c,e){a.start=b;a.end=c;a.units=e;a.value=c.getTime()-b.getTime();if(0>a.value)var d=c,c=b,b=d;a.refMonth=new Date(b.getFullYear(),b.getMonth(),15);try{a.millennia=0;a.centuries=0;a.decades=0;a.years=c.getUTCFullYear()-b.getUTCFullYear();a.months=c.getUTCMonth()-b.getUTCMonth();a.weeks=0;a.days=c.getUTCDate()-
b.getUTCDate();a.hours=c.getUTCHours()-b.getUTCHours();a.minutes=c.getUTCMinutes()-b.getUTCMinutes();a.seconds=c.getUTCSeconds()-b.getUTCSeconds();a.milliseconds=c.getUTCMilliseconds()-b.getUTCMilliseconds();var f;0>a.milliseconds?(f=j(-a.milliseconds/1E3),a.seconds-=f,a.milliseconds+=1E3*f):1E3<=a.milliseconds&&(a.seconds+=h(a.milliseconds/1E3),a.milliseconds%=1E3);0>a.seconds?(f=j(-a.seconds/60),a.minutes-=f,a.seconds+=60*f):60<=a.seconds&&(a.minutes+=h(a.seconds/60),a.seconds%=60);0>a.minutes?
(f=j(-a.minutes/60),a.hours-=f,a.minutes+=60*f):60<=a.minutes&&(a.hours+=h(a.minutes/60),a.minutes%=60);0>a.hours?(f=j(-a.hours/24),a.days-=f,a.hours+=24*f):24<=a.hours&&(a.days+=h(a.hours/24),a.hours%=24);for(;0>a.days;)a.months--,a.days+=m(a.refMonth,1);7<=a.days&&(a.weeks+=h(a.days/7),a.days%=7);0>a.months?(f=j(-a.months/12),a.years-=f,a.months+=12*f):12<=a.months&&(a.years+=h(a.months/12),a.months%=12);10<=a.years&&(a.decades+=h(a.years/10),a.years%=10,10<=a.decades&&(a.centuries+=h(a.decades/
10),a.decades%=10,10<=a.centuries&&(a.millennia+=h(a.centuries/10),a.centuries%=10)));e&1024||(a.centuries+=10*a.millennia,delete a.millennia);e&512||(a.decades+=10*a.centuries,delete a.centuries);e&256||(a.years+=10*a.decades,delete a.decades);e&128||(a.months+=12*a.years,delete a.years);!(e&64)&&a.months&&(a.days+=m(a.refMonth,a.months),delete a.months,7<=a.days&&(a.weeks+=h(a.days/7),a.days%=7));e&32||(a.days+=7*a.weeks,delete a.weeks);e&16||(a.hours+=24*a.days,delete a.days);e&8||(a.minutes+=
60*a.hours,delete a.hours);e&4||(a.seconds+=60*a.minutes,delete a.minutes);e&2||(a.milliseconds+=1E3*a.seconds,delete a.seconds);e&1||delete a.milliseconds}finally{delete a.refMonth}return a}function d(a,b,c){var e,c=c||222;"function"===typeof a?(e=a,a=null):a instanceof Date||(a=null!==a&&isFinite(a)?new Date(a):null);"function"===typeof b?(e=b,b=null):b instanceof Date||(b=null!==b&&isFinite(b)?new Date(b):null);if(!a&&!b)return new i;if(!e)return n(new i,a||new Date,b||new Date,c);var d;d=c&1?
1E3/30:c&2?1E3:c&4?6E4:c&8?36E5:c&16?864E5:6048E5;var f=function(){e(n(new i,a||new Date,b||new Date,c))};f();return setInterval(f,d)}var j=Math.ceil,h=Math.floor,o=Math.round,k;i.prototype.toString=function(a){var b=k(this);b.length>a&&(b=b.slice(0,a));a=b.length;if(!a)return"";1<a&&(b[a-1]="and "+b[a-1]);return b.join(", ")};i.prototype.toHTML=function(a,b){var a=a||"span",c=k(this);c.length>b&&(c=c.slice(0,b));var e=c.length;if(!e)return"";for(var d=0;d<e;d++)c[d]="<"+a+">"+c[d]+"</"+a+">";--e&&
(c[e]="and "+c[e]);return c.join(", ")};i.prototype.toShort=function(a){var b=k(this),a=0<a?a:1;b.length>a&&(b=b.slice(0,a));a=b.length;if(!a)return"";1<a&&(b[a-1]="and "+b[a-1]);return b.join(", ")};k=function(a){var b=[];a.millennia&&b.push(g(a.millennia,"millennium","millennia"));a.centuries&&b.push(g(a.centuries,"century","centuries"));a.decades&&b.push(g(a.decades,"decade","decades"));a.years&&b.push(g(a.years,"year","years"));a.months&&b.push(g(a.months,"month","months"));a.weeks&&b.push(g(a.weeks,
"week","weeks"));a.days&&b.push(g(a.days,"day","days"));a.hours&&b.push(g(a.hours,"hour","hours"));a.minutes&&b.push(g(a.minutes,"minute","minutes"));a.seconds&&b.push(g(a.seconds,"second","seconds"));a.milliseconds&&b.push(g(a.milliseconds,"millisecond","milliseconds"));return b};d.MILLISECONDS=1;d.SECONDS=2;d.MINUTES=4;d.HOURS=8;d.DAYS=16;d.WEEKS=32;d.MONTHS=64;d.YEARS=128;d.DECADES=256;d.CENTURIES=512;d.MILLENNIA=1024;d.DEFAULTS=222;d.ALL=2047;l&&l.exports&&(l.exports=d);return d}(module);
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>countdown</title>
<style>
@import url(http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
#counter {
font-size: 300%;
font-weight: bold;
font-family: Yanone Kaffeesatz;
text-align: center;
white-space: nowrap;
}
</style>
</head>
<body>
<h1 id="counter">countdown</h1>
<script src="countdown.min.js" type="text/javascript"></script>
<script type="text/javascript">
(function() {
var end = window.location.hash.substr(1) * 1000;
var ts = countdown(end, function(ts) {
document.getElementById('counter').innerHTML = ts.toHTML('strong');
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment