Created
August 24, 2010 19:19
-
-
Save chrisdickinson/548141 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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