Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created August 31, 2010 17:52
Show Gist options
  • Save jimfleming/559427 to your computer and use it in GitHub Desktop.
Save jimfleming/559427 to your computer and use it in GitHub Desktop.
var Timespan = function(milliseconds) {
this.milliseconds = milliseconds;
this.seconds = this.milliseconds / 1000;
this.minutes = this.seconds / 60;
this.hours = this.minutes / 60;
this.days = this.hours / 24;
this.weeks = this.days / 7;
this.months = this.days / 30;
this.years = this.months / 12;
this.centuries = this.years / 100;
this.millenia = this.years / 1000;
return this;
};
Timespan.zero_pad = function(value, length) {
value = value.toString();
while (value.length < length)
value = '0' + value;
return value;
};
Timespan.prototype.toString = function() {
return Timespan.zero_pad(Math.floor(this.days), 2) + ':'
+ Timespan.zero_pad(Math.floor(this.hours) % 24, 2) + ':'
+ Timespan.zero_pad(Math.floor(this.minutes) % 60, 2) + ':'
+ Timespan.zero_pad(Math.floor(this.seconds) % 60, 2) + '.'
+ Timespan.zero_pad(Math.floor(this.milliseconds) % 1000, 3);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment