Skip to content

Instantly share code, notes, and snippets.

@dlsym
Created February 15, 2012 22:15
Show Gist options
  • Save dlsym/1839402 to your computer and use it in GitHub Desktop.
Save dlsym/1839402 to your computer and use it in GitHub Desktop.
var TeaTime = function( year, month, day, hour, minute, second ) {
this.year = year; this.month = month; this.day = day;
this.hour = hour; this.minute = minute; this.second = second;
var start = new Date(
this.year, this.month-1, this.day,
this.hour, this.minute, this.second
);
var now = new Date();
this.diff = now.getTime() - start.getTime();
}
TeaTime.prototype.getElapsedTime = function() {
var diff = this.diff / 1000;
var dH = parseInt(diff / 3600);
var dM = parseInt((diff / 60) % 60);
var dS = parseInt(diff % 60);
return [dH, dM, dS];
}
TeaTime.prototype.getFormattedTimer = function() {
var dT = this.getElapsedTime();
var fmt_pad = function( num, pad ) {
var res = '' + num; // to_string
while( res.length < pad ) {
res = '0' + res;
}
return res;
}
var res = fmt_pad( dT[0], 2 ) + ':' +
fmt_pad( dT[1], 2 ) + ':' +
fmt_pad( dT[2], 2 );
return res;
}
var time = new TeaTime( 2012, 2, 15, 20, 0, 0 );
console.log( time.getElapsedTime() );
console.log( time.getFormattedTimer() );
// (Now: ... 03:13:48 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment