Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created August 24, 2010 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdickinson/548141 to your computer and use it in GitHub Desktop.
Save chrisdickinson/548141 to your computer and use it in GitHub Desktop.
var Difference = function(milliseconds) {
if(milliseconds instanceof Object) {
var output = 0;
output += milliseconds.days === undefined ? 0 : 86400000;
output += milliseconds.hours === undefined ? 0 : 3600000;
output += milliseconds.minutes === undefined ? 0 : 60000;
output += milliseconds.seconds === undefined ? 0 : 1000;
output += milliseconds.milliseconds === undefined ? 0 : milliseconds;
milliseconds = output;
}
this._baseMilliseconds = milliseconds;
};
Difference.prototype.days = function() {
return parseInt(this._baseMilliseconds / 86400000, 10);
};
Difference.prototype.hours = function() {
return parseInt(this._baseMilliseconds / 3600000, 10);
};
Difference.prototype.minutes = function() {
return parseInt(this._baseMilliseconds / 60000, 10);
};
Difference.prototype.seconds = function() {
return parseInt(this._baseMilliseconds / 1000, 10);
};
Difference.prototype.valueOf = function() {
return this._baseMilliseconds;
};
var timedelta = function(input) {
return new Difference(input);
};
Date.prototype.delta = function(rhs) {
return timedelta(this - rhs);
};
Date.prototype.add = function(delta) {
return new Date((this.valueOf()) + (delta.valueOf()));
};
var sys = require('sys');
var a = new Date();
setTimeout(function() {
var b = new Date(),
delta = b.delta(a);
sys.puts("minutes", delta.minutes());
sys.puts("seconds", delta.seconds());
sys.puts("days", delta.hours());
sys.puts(b.add(timedelta({'days':1})));
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment