From: Tim Wood Date: Sat, 5 Mar 2011 03:11:17 +0000 (-0800) Subject: Second draft of project. X-Git-Tag: 0.3.0~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5164c0a06e34f68663e84230506a95e44e84db4;p=thirdparty%2Fmoment.git Second draft of project. Modified dateFormat to use Date.prototype.humanize Changed the input format to be less like php date() Changed name from '_.timeNow' to '_.now' Changed name from '_.timeAsWords' to '_.relativetime' Changed name from '_.timeDiff' to '_.msapart' Changed name from '_.timeFromNow' to '_.fromnow' Changed name from '_.setTimeI18N' to '_.customizedate' --- diff --git a/lib/underscore.date.js b/lib/underscore.date.js index 3656c4112..9143d4f78 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -3,330 +3,224 @@ // (c) 2011 Tim Wood // Underscore.date is freely distributable under the terms of the MIT license. // -// Version 0.1.0 +// Version 0.2.0 +/*global _:false */ -(function(){ - // ------------------------- Baseline setup --------------------------------- - // Establish the root object, "window" in the browser, or "global" on the server. - var root = this, - _d, - dateToFormat, - formatStrings, - charactersToReplace = /\\?([a-z])/gi, - wordsWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], +(function(Date, _, undefined){ + // assign variables here so they can be overwritten for i18n or customization + var self = this, _d, wordsMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], + wordsWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], wordsTimeAgo = { future: "in %s", past: "%s ago", - ss: "less than a minute", + S: "less than a minute", m: "about a minute", - mm: "%d minutes", + M: "%d minutes", h: "about an hour", - hh: "about %d hours", + H: "about %d hours", d: "a day", - dd: "%d days", - n: "about a month", - nn: "%d months", + D: "%d days", + l: "about a month", + L: "%d months", y: "about a year", - yy: "%d years" + Y: "%d years" }; + // add ordinal to number function createOrdinal(number) { return ((number / 10 | 0) === 1) ? 'th' : (number % 10 === 1) ? 'st' : (number % 10 === 2) ? 'nd' : (number % 10 === 3) ? 'rd' : 'th'; - }; - // check if the character is a format - // return formatted string or non string. - function replaceFunction(input) { - return formatStrings[input] ? formatStrings[input]() : input; - }; - - // zero fill a number + } + + // left zero fill a number function leftZeroFill(number, targetLength) { var output = number + ''; while (output.length < targetLength) { output = '0' + output; } return output; - }; + } - // strings to consider when formatting - formatStrings = { - - // DAY OF MONTH - - // day number of month - // 1 .. 31 - j: function () { // 1..31 - return dateToFormat.getDate(); - }, - // day number of month (leading zero) - // 01 .. 31 - d: function () { // 01..31 - return leftZeroFill(formatStrings.j(), 2); - }, - // day number of month suffix - // st, nd, rd, th - S: function () { - return createOrdinal(formatStrings.j()); - }, - - // DAY OF WEEK - - // day name of week - // Monday .. Sunday - l: function () { - return wordsWeekdays[formatStrings.w()]; - }, - // day name of week (shorthand) - // Mon .. Sun - D: function () { - return formatStrings.l().slice(0, 3); - }, - // day number of week - // 0[Sun] .. 6[Sat] - w: function () { - return dateToFormat.getDay(); - }, - // day number of week (ISO-8601) - // 1[Mon] .. 7[Sun] - N: function () { - return formatStrings.w() || 7; - }, - - // DAY OF YEAR - - // day number of year - // 0 .. 365 - z: function () { - var a = new Date(formatStrings.Y(), formatStrings.n() - 1, formatStrings.j()), - b = new Date(formatStrings.Y(), 0, 1); - return ((a - b) / 864e5) + .5 | 0; - }, - - // WEEK - - // week number (ISO-8601) - // 1 .. 52 - W: function () { - var a = new Date(formatStrings.Y(), formatStrings.n() - 1, formatStrings.j() - formatStrings.N() + 4), - b = new Date(a.getFullYear(), 0, 4); - return leftZeroFill((a - b) / 864e5 / 7 + 1.5 | 0, 2); - }, - - // MONTH - - // month number - // 1 .. 12 - n: function () { - return dateToFormat.getMonth() + 1; - }, - // month number (leading zero) - // 01 .. 12 - m: function () { - return leftZeroFill(formatStrings.n(), 2); - }, - // month name - // January .. December - F: function () { - return wordsMonths[formatStrings.n() - 1]; - }, - // month name (shorthand) - // Jan .. Dec - M: function () { - return formatStrings.F().slice(0, 3); - }, - // days in month - // 28 .. 31 - t: function () { - return (new Date(formatStrings.Y(), formatStrings.n(), 0)).getDate(); - }, - - // YEAR - - // is leap year - // 0 or 1 - L: function () { - return new Date(formatStrings.Y(), 1, 29).getMonth() === 1 | 0; - }, - // year number (all 4 digits) - // 1980 .. 2010 - Y: function () { - return dateToFormat.getFullYear(); - }, - // year number (last 2 digits) - // 00 .. 99 - y: function () { - return (formatStrings.Y() + "").slice(-2); - }, - - // TIME - - // am / pm (lowercase) - // am or pm - a: function () { - return dateToFormat.getHours() > 11 ? 'pm' : 'am'; - }, - // am / pm (uppercase) - // AM or PM - A: function () { - return formatStrings.a().toUpperCase(); - }, - // swatch internet time - // 000 .. 999 - B: function () { - var H = dateToFormat.getUTCHours() * 3600, // hours - i = dateToFormat.getUTCMinutes() * 60, // minutes - s = dateToFormat.getUTCSeconds(); // seconds - return leftZeroFill(((H + i + s + 3600) / 86.4) % 1e3 | 0, 3); - }, - - // HOURS - - // 12 hours - // 1 .. 12 - g: function () { // 12-Hours; 1..12 - return formatStrings.G() % 12 || 12; - }, - // 12 hours (leading zero) - // 01 .. 12 - h: function () { - return leftZeroFill(formatStrings.g(), 2); - }, - // 24 hours - // 0 .. 23 - G: function () { - return dateToFormat.getHours(); - }, - // 24 hours (leading zero) - // 00 .. 23 - H: function () { - return leftZeroFill(formatStrings.G(), 2); - }, - - // MINUTES SECONDS MILLISECONDS - - // minutes (leading zero) - // 00 .. 59 - i: function () { - return leftZeroFill(dateToFormat.getMinutes(), 2); - }, - // seconds (leading zero) - // 00 .. 59 - s: function () { - return leftZeroFill(dateToFormat.getSeconds(), 2); - }, - // seconds (since UNIX epoch) - // 0 .. 1342759768 - U: function () { - return dateToFormat.getTime() / 1000 | 0; - }, - - // TIMEZONE - - // DST observed - // 0 or 1 - I: function () { - var year = formatStrings.Y(); - var offset = new Date(year, 0, 1).getTimezoneOffset(); - offset = Math.max(offset, new Date(year, 6, 1).getTimezoneOffset()); - return 0 + (dateToFormat.getTimezoneOffset() < offset); - }, - // difference to GMT (hour format) - // +0700 - O: function () { // Difference to GMT in hour format; e.g. +0200 - var a = dateToFormat.getTimezoneOffset(); - return (a > 0 ? "-" : "+") + leftZeroFill(Math.abs(a / 60 * 100), 4); - }, - // difference to GMT (hour format with colon) - // +07:00 - P: function () { - var O = formatStrings.O(); - return (O.substr(0, 3) + ":" + O.substr(3, 2)); - }, - // difference to GMT (seconds) - // -43200 .. 50400 - Z: function () { - return -dateToFormat.getTimezoneOffset() * 60; + // add zero fill and ordinal to number + function zeroFillAndOrdinal(number, zerofill, ordinal) { + var output = zerofill ? leftZeroFill(number, zerofill) : number; + return ordinal ? output + createOrdinal(number) : output; + } + + // Date.prototype.humanize + function humanize(inputString) { + // shortcuts to this and getting time functions + // done to save bytes in minification + var self = this, + currentMonth = self.getMonth(), + currentDate = self.getDate(), + currentYear = self.getFullYear(), + currentDay = self.getDay(), + currentHours = self.getHours(), + currentMinutes = self.getMinutes(), + currentSeconds = self.getSeconds(), + charactersToReplace = /[a-z][0-9]?/gi, + formatFunctions = { + // MONTH + // number + L : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentMonth + 1, zerofill ? 2 : 0, ordinal); + }, + // string + l : function(shorthand) { + var output = wordsMonths[currentMonth]; + return shorthand ? output.slice(0, 3) : output; + + }, + + // DAY OF MONTH + // number + D : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentDate, zerofill ? 2 : 0, ordinal); + }, + + // DAY OF YEAR + // number + d : function(ordinal, zerofill) { + var a = new Date(currentYear, currentMonth, currentDate), + b = new Date(currentYear, 0, 1); + return zeroFillAndOrdinal(((a - b) / 864e5) + 1.5 | 0, zerofill ? 3 : 0, ordinal); + }, + + // WEEKDAY + // number + W : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentDay, zerofill ? 2 : 0, ordinal); + }, + // string + w : function(shorthand) { + var output = wordsWeekdays[currentDay]; + return shorthand ? output.slice(0, 3) : output; + }, + + // WEEK OF YEAR + K : function(ordinal, zerofill) { + var a = new Date(currentYear, currentMonth, currentDate - currentDay + 5), + b = new Date(a.getFullYear(), 0, 4); + return zeroFillAndOrdinal((a - b) / 864e5 / 7 + 1.5 | 0, zerofill ? 2 : 0, ordinal); + }, + + // YEAR + Y : function(shorthand) { + return shorthand ? (currentYear + '').slice(-2) : currentYear; + }, + + // AM / PM + a : function() { + return currentHours > 11 ? 'pm' : 'am'; + }, + A : function() { + return currentHours > 11 ? 'PM' : 'AM'; + }, + + // HOUR + // 24 + H : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentHours, zerofill ? 2 : 0, ordinal); + }, + // 12 + h : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentHours % 12 || 12, zerofill ? 2 : 0, ordinal); + }, + + // MINUTE + m : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentMinutes, zerofill ? 2 : 0, ordinal); + }, + + // SECOND + s : function(ordinal, zerofill) { + return zeroFillAndOrdinal(currentSeconds, zerofill ? 2 : 0, ordinal); + } + }; + + // check if the character is a format + // return formatted string or non string. + function replaceFunction(input) { + var character = input.charAt(0), + parameter = input.charAt(1) || 0; + return formatFunctions[character] ? formatFunctions[character](parameter & 1, parameter >> 1) : input; } - }; + + return inputString.replace(charactersToReplace, replaceFunction); + } + + /* + * Underscore mixins + */ + function makeInputMilliseconds(input){ + return input === undefined ? _d.now(true) : + input instanceof Date ? input.getTime() : input; + } function substituteTimeAgo(string, number) { - number = number || 1; - string = wordsTimeAgo[string] || string; - return string.replace(/%d/i, number); + return wordsTimeAgo[string].replace(/%d/i, number || 1); } - function makeInputADate(input){ - return typeof input === 'undefined' ? _d.dateNow() : - input instanceof Date ? input : - new Date(input); - } - - var _d = { - dateNow : function(asTimestamp) { + _d = { + now : function(asTimestamp) { return asTimestamp ? new Date().getTime() : new Date(); }, - dateFormat : function(format, _time) { - var format = format || ''; - dateToFormat = makeInputADate(_time); - return format.replace(charactersToReplace, replaceFunction); - }, - timeAsWords : function(milliseconds) { - var seconds = Math.abs(milliseconds) / 1000, + relativetime : function(milliseconds) { + var seconds = Math.abs(makeInputMilliseconds(milliseconds)) / 1000, minutes = seconds / 60, hours = minutes / 60, days = hours / 24, years = days / 365; - return seconds < 45 && substituteTimeAgo('ss', seconds | 0) || + return seconds < 45 && substituteTimeAgo('S', seconds | 0) || seconds < 90 && substituteTimeAgo('m') || - minutes < 45 && substituteTimeAgo('mm', minutes | 0) || + minutes < 45 && substituteTimeAgo('M', minutes | 0) || minutes < 90 && substituteTimeAgo('h') || - hours < 24 && substituteTimeAgo('hh', hours | 0) || + hours < 24 && substituteTimeAgo('H', hours | 0) || hours < 48 && substituteTimeAgo('d') || - days < 30 && substituteTimeAgo('dd', days | 0) || - days < 60 && substituteTimeAgo('n') || - days < 350 && substituteTimeAgo('nn', days / 30 | 0) || + days < 30 && substituteTimeAgo('D', days | 0) || + days < 60 && substituteTimeAgo('l') || + days < 350 && substituteTimeAgo('L', days / 30 | 0) || years < 2 && substituteTimeAgo('y') || - substituteTimeAgo('yy', years | 0); + substituteTimeAgo('Y', years | 0); }, - timeDiff : function(_time, _now) { - var time = makeInputADate(_time), - now = makeInputADate(_now); - return time.getTime() - now.getTime(); + msapart : function(time, now) { + return makeInputMilliseconds(time) - makeInputMilliseconds(now); }, - timeFromNow : function(_time, _now) { - var difference = _d.timeDiff(_time, _now), + fromnow : function(time, now) { + var difference = _d.msapart(time, now), string = difference < 0 ? wordsTimeAgo.past : wordsTimeAgo.future; - return string.replace(/%s/i, _d.timeAsWords(difference)); + return string.replace(/%s/i, _d.relativetime(difference)); }, - setTimeI18N : function(i18n) { - if (i18n.weekdays && _.isArray(i18n.weekdays) && i18n.weekdays.length == 7) { - wordsWeekdays = i18n.weekdays; + customizedate : function(input) { + if (input.weekdays && _.isArray(input.weekdays) && input.weekdays.length === 7) { + wordsWeekdays = input.weekdays; } - if (i18n.months && _.isArray(i18n.months) && i18n.months.length == 12) { - wordsMonths = i18n.months; + if (input.months && _.isArray(input.months) && input.months.length === 12) { + wordsMonths = input.months; } - if (i18n.timeago) { - _.extend(wordsTimeAgo, i18n.timeago); + if (input.timeago) { + _.extend(wordsTimeAgo, input.timeago); } - if (i18n.ordinal && _.isFunction(i18n.ordinal)) { - createOrdinal = i18n.ordinal; + if (input.ordinal && _.isFunction(input.ordinal)) { + createOrdinal = input.ordinal; } } }; - // make global object - root._d = _d; + // assign to prototype + Date.prototype.humanize = humanize; + + // assign to global + self._d = _d; // integrate with underscore.js - if (root._) { - root._.mixin(_d); + if (self._) { + self._.mixin(_d); } - - -}()); +}(Date, _)); diff --git a/lib/underscore.date.min.js b/lib/underscore.date.min.js index 643a01c8d..e52acd3ea 100644 --- a/lib/underscore.date.min.js +++ b/lib/underscore.date.min.js @@ -3,6 +3,6 @@ // (c) 2011 Tim Wood // Underscore.date is freely distributable under the terms of the MIT license. // -// Version 0.1.0 +// Version 0.2.0 -(function(){function m(a){return typeof a=="undefined"?b.dateNow():a instanceof Date?a:new Date(a)}function l(a,b){b=b||1,a=h[a]||a;return a.replace(/%d/i,b)}function k(a,b){var c=a+"";while(c.length11?"pm":"am"},A:function(){return d.a().toUpperCase()},B:function(){var a=c.getUTCHours()*3600,b=c.getUTCMinutes()*60,d=c.getUTCSeconds();return k((a+b+d+3600)/86.4%1e3|0,3)},g:function(){return d.G()%12||12},h:function(){return k(d.g(),2)},G:function(){return c.getHours()},H:function(){return k(d.G(),2)},i:function(){return k(c.getMinutes(),2)},s:function(){return k(c.getSeconds(),2)},U:function(){return c.getTime()/1e3|0},I:function(){var a=d.Y(),b=(new Date(a,0,1)).getTimezoneOffset();b=Math.max(b,(new Date(a,6,1)).getTimezoneOffset());return 0+(c.getTimezoneOffset()0?"-":"+")+k(Math.abs(a/60*100),4)},P:function(){var a=d.O();return a.substr(0,3)+":"+a.substr(3,2)},Z:function(){return-c.getTimezoneOffset()*60}};var b={dateNow:function(a){return a?(new Date).getTime():new Date},dateFormat:function(a,b){var a=a||"";c=m(b);return a.replace(e,j)},timeAsWords:function(a){var b=Math.abs(a)/1e3,c=b/60,d=c/60,e=d/24,f=e/365;return b<45&&l("ss",b|0)||b<90&&l("m")||c<45&&l("mm",c|0)||c<90&&l("h")||d<24&&l("hh",d|0)||d<48&&l("d")||e<30&&l("dd",e|0)||e<60&&l("n")||e<350&&l("nn",e/30|0)||f<2&&l("y")||l("yy",f|0)},timeDiff:function(a,b){var c=m(a),d=m(b);return c.getTime()-d.getTime()},timeFromNow:function(a,c){var d=b.timeDiff(a,c),e=d<0?h.past:h.future;return e.replace(/%s/i,b.timeAsWords(d))},setTimeI18N:function(a){a.weekdays&&_.isArray(a.weekdays)&&a.weekdays.length==7&&(f=a.weekdays),a.months&&_.isArray(a.months)&&a.months.length==12&&(g=a.months),a.timeago&&_.extend(h,a.timeago),a.ordinal&&_.isFunction(a.ordinal)&&(i=a.ordinal)}};a._d=b,a._&&a._.mixin(b)})() \ No newline at end of file +function(a,b,c){function n(a,b){return h[a].replace(/%d/i,b||1)}function m(b){return b===c?e.now(!0):b instanceof a?b.getTime():b}function l(b){function p(a){var b=a.charAt(0),c=a.charAt(1)||0;return o[b]?o[b](c&1,c>>1):a}var c=this,d=c.getMonth(),e=c.getDate(),h=c.getFullYear(),i=c.getDay(),j=c.getHours(),l=c.getMinutes(),m=c.getSeconds(),n=/[a-z][0-9]?/gi,o={L:function(a,b){return k(d+1,b?2:0,a)},l:function(a){var b=f[d];return a?b.slice(0,3):b},D:function(a,b){return k(e,b?2:0,a)},d:function(b,c){var f=new a(h,d,e),g=new a(h,0,1);return k((f-g)/864e5+1.5|0,c?3:0,b)},W:function(a,b){return k(i,b?2:0,a)},w:function(a){var b=g[i];return a?b.slice(0,3):b},K:function(b,c){var f=new a(h,d,e-i+5),g=new a(f.getFullYear(),0,4);return k((f-g)/864e5/7+1.5|0,c?2:0,b)},Y:function(a){return a?(h+"").slice(-2):h},a:function(){return j>11?"pm":"am"},A:function(){return j>11?"PM":"AM"},H:function(a,b){return k(j,b?2:0,a)},h:function(a,b){return k(j%12||12,b?2:0,a)},m:function(a,b){return k(l,b?2:0,a)},s:function(a,b){return k(m,b?2:0,a)}};return b.replace(n,p)}function k(a,b,c){var d=b?j(a,b):a;return c?d+i(a):d}function j(a,b){var c=a+"";while(c.length Sunday, February 14th 2010, 3:25:50 pm', function() { + return date1.humanize("w, l D1 Y, h:m2:s2 a"); }); - - JSLitmus.test('_.timeDiff(1000)', function() { - return _.timeDiff(1000); + + JSLitmus.test('Date.humanize => Sun, 3PM', function() { + return date1.humanize("w1, hA"); + }); + + JSLitmus.test('_.fromnow(1000 * 30, 0)', function() { + return _.fromnow(1000, 0); }); - JSLitmus.test('_.timeDiff(date1, date2)', function() { - return _.timeDiff(date1, new Date(1000)); + JSLitmus.test('_.relativetime(1000 * 30)', function() { + return _.relativetime(rt2); }); - JSLitmus.test('_.timeDiff(date1, 1000)', function() { - return _.timeDiff(date1, 1000); + JSLitmus.test('_.relativetime(1000 * 60 * 60 * 24 * 365 * 5)', function() { + return _.relativetime(rt1); }); - JSLitmus.test('_.timeDiff(1000, 1000)', function() { - return _.timeDiff(1000, 1000); + JSLitmus.test('_.msapart(1000)', function() { + return _.msapart(1000); }); - JSLitmus.test('_.dateFormat("LMAO")', function() { - return _.dateFormat("LMAO"); + JSLitmus.test('_.msapart(date1, date2)', function() { + return _.msapart(date1, date2); }); - JSLitmus.test('_.dateFormat("ABDFGHILMNOPSUWYZadghijlmnstwyz")', function() { - return _.dateFormat("ABDFGHILMNOPSUWYZadghijlmnstwyz"); + JSLitmus.test('_.msapart(date1, 1000)', function() { + return _.msapart(date1, 1000); }); + JSLitmus.test('_.msapart(1000, 1000)', function() { + return _.msapart(1000, 1000); + }); + })(); \ No newline at end of file diff --git a/test/test.html b/test/test.html index cb0fa7564..edb0de721 100644 --- a/test/test.html +++ b/test/test.html @@ -9,8 +9,8 @@ - +

underscore.date.js test suite

diff --git a/test/time.js b/test/time.js index dc61fdfa8..0ac2b79de 100644 --- a/test/time.js +++ b/test/time.js @@ -2,37 +2,37 @@ $(document).ready(function() { module("Time"); - test("timeAsWords", function() { + test("relativetime", function() { expect(11); - equal(_.timeAsWords(1000 * 30), "less than a minute"); - equal(_.timeAsWords(1000 * 60), "about a minute"); - equal(_.timeAsWords(1000 * 60 * 5), "5 minutes"); - equal(_.timeAsWords(1000 * 60 * 60), "about an hour"); - equal(_.timeAsWords(1000 * 60 * 60 * 5), "about 5 hours"); - equal(_.timeAsWords(1000 * 60 * 60 * 24), "a day"); - equal(_.timeAsWords(1000 * 60 * 60 * 24 * 5), "5 days"); - equal(_.timeAsWords(1000 * 60 * 60 * 24 * 30), "about a month"); - equal(_.timeAsWords(1000 * 60 * 60 * 24 * 30 * 5), "5 months"); - equal(_.timeAsWords(1000 * 60 * 60 * 24 * 30 * 12), "about a year"); - equal(_.timeAsWords(1000 * 60 * 60 * 24 * 365 * 5), "5 years"); + equal(_.relativetime(1000 * 30), "less than a minute"); + equal(_.relativetime(1000 * 60), "about a minute"); + equal(_.relativetime(1000 * 60 * 5), "5 minutes"); + equal(_.relativetime(1000 * 60 * 60), "about an hour"); + equal(_.relativetime(1000 * 60 * 60 * 5), "about 5 hours"); + equal(_.relativetime(1000 * 60 * 60 * 24), "a day"); + equal(_.relativetime(1000 * 60 * 60 * 24 * 5), "5 days"); + equal(_.relativetime(1000 * 60 * 60 * 24 * 30), "about a month"); + equal(_.relativetime(1000 * 60 * 60 * 24 * 30 * 5), "5 months"); + equal(_.relativetime(1000 * 60 * 60 * 24 * 30 * 12), "about a year"); + equal(_.relativetime(1000 * 60 * 60 * 24 * 365 * 5), "5 years"); }); - test("timeDiff", function() { + test("msapart", function() { expect(5); - equal(_d.timeDiff(1000, 0), 1000, "1 second (ms) - 0 = 1000"); - equal(_d.timeDiff(1000, 500), 500, "1 second (ms) - .5 second (ms) = -500"); - equal(_d.timeDiff(0, 1000), -1000, "0 - 1 second (ms) = -1000"); - equal(_d.timeDiff(new Date(1000), 1000), 0, "1 second (date) - 1 second (ms) = 0"); + equal(_d.msapart(1000, 0), 1000, "1 second (ms) - 0 = 1000"); + equal(_d.msapart(1000, 500), 500, "1 second (ms) - .5 second (ms) = -500"); + equal(_d.msapart(0, 1000), -1000, "0 - 1 second (ms) = -1000"); + equal(_d.msapart(new Date(1000), 1000), 0, "1 second (date) - 1 second (ms) = 0"); var oneHourDate = new Date(), nowDate = new Date(); oneHourDate.setHours(oneHourDate.getHours() + 1); - equal(_d.timeDiff(oneHourDate, nowDate), 60 * 60 * 1000, "1 hour from now = 360000"); + equal(_d.msapart(oneHourDate, nowDate), 60 * 60 * 1000, "1 hour from now = 360000"); }); - test("timeFromNow", function() { + test("fromnow", function() { expect(2); - equal(_d.timeFromNow(30000, 0), "in less than a minute"); - equal(_d.timeFromNow(0, 30000), "less than a minute ago"); + equal(_d.fromnow(30000, 0), "in less than a minute"); + equal(_d.fromnow(0, 30000), "less than a minute ago"); });