});
-test("milliseconds", 5, function() {
- equal(_date(1000).from(0, true, true), 1000, "1 second - 0 = 1000");
- equal(_date(1000).from(500, false, true), 500, "1 second - .5 second = -500");
- equal(_date(0).from(1000, false, true), -1000, "0 - 1 second = -1000");
- equal(_date(new Date(1000)).from(1000, false, true), 0, "1 second - 1 second = 0");
- var oneHourDate = new Date(),
- nowDate = new Date();
- oneHourDate.setHours(oneHourDate.getHours() + 1);
- equal(_date(oneHourDate).from(nowDate, false, true), 60 * 60 * 1000, "1 hour from now = 360000");
-});
-
-
test("suffix", 2, function() {
equal(_date(30000).from(0), "in seconds", "prefix");
equal(_date(0).from(30000), "seconds ago", "suffix");
});
+module("diff");
+
+
+test("diff", 5, function() {
+ equal(_date(1000).diff(0), 1000, "1 second - 0 = 1000");
+ equal(_date(1000).diff(500), 500, "1 second - .5 second = -500");
+ equal(_date(0).diff(1000), -1000, "0 - 1 second = -1000");
+ equal(_date(new Date(1000)).diff(1000), 0, "1 second - 1 second = 0");
+ var oneHourDate = new Date(),
+ nowDate = new Date();
+ oneHourDate.setHours(oneHourDate.getHours() + 1);
+ equal(_date(oneHourDate).diff(nowDate), 60 * 60 * 1000, "1 hour from now = 360000");
+});
+
module("leap year");
// test 'test'
testLang(test, testFrom);
+
+ // switch back to en to prevent other tests from failing
+ _date.lang('en');
});
\ No newline at end of file
}
});
- // convert any input to milliseconds
- function makeInputMilliseconds(input) {
- return isNaN(input) ? new UnderscoreDate(input).date.getTime() : input;
- }
-
// helper function for _date.from() and _date.fromNow()
function substituteTimeAgo(string, number) {
return _date.relativeTime[string].replace(/%d/i, number || 1);
}
- function msApart(time, now) {
- return makeInputMilliseconds(time) - makeInputMilliseconds(now);
- }
-
function relativeTime(milliseconds) {
var seconds = Math.abs(milliseconds) / 1000,
minutes = seconds / 60,
return this;
},
- from : function (time, withoutSuffix, asMilliseconds) {
- var difference = msApart(this.date, time),
- string = difference < 0 ? _date.relativeTime.past : _date.relativeTime.future;
- return asMilliseconds ? difference :
- withoutSuffix ? relativeTime(difference) :
- string.replace(/%s/i, relativeTime(difference));
+ diff : function (input, format) {
+ return this.date - _date(input, format).date;
+ },
+
+ from : function (time, withoutSuffix) {
+ var difference = this.diff(time),
+ string = difference < 0 ? _date.relativeTime.past : _date.relativeTime.future,
+ output = relativeTime(difference)
+ return withoutSuffix ? output : string.replace(/%s/i, output);
},
- fromNow : function (withoutSuffix, asMilliseconds) {
- return this.from(new UnderscoreDate(), withoutSuffix, asMilliseconds);
+ fromNow : function (withoutSuffix) {
+ return this.from(new UnderscoreDate(), withoutSuffix);
},
isLeapYear : function () {