// (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, _));
// (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.length<b)c="0"+c;return c}function j(a){return d[a]?d[a]():a}function i(a){return(a/10|0)===1?"th":a%10===1?"st":a%10===2?"nd":a%10===3?"rd":"th"}var a=this,b,c,d,e=/\\?([a-z])/gi,f=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],g=["January","February","March","April","May","June","July","August","September","October","November","December"],h={future:"in %s",past:"%s ago",ss:"less than a minute",m:"about a minute",mm:"%d minutes",h:"about an hour",hh:"about %d hours",d:"a day",dd:"%d days",n:"about a month",nn:"%d months",y:"about a year",yy:"%d years"};d={j:function(){return c.getDate()},d:function(){return k(d.j(),2)},S:function(){return i(d.j())},l:function(){return f[d.w()]},D:function(){return d.l().slice(0,3)},w:function(){return c.getDay()},N:function(){return d.w()||7},z:function(){var a=new Date(d.Y(),d.n()-1,d.j()),b=new Date(d.Y(),0,1);return(a-b)/864e5+.5|0},W:function(){var a=new Date(d.Y(),d.n()-1,d.j()-d.N()+4),b=new Date(a.getFullYear(),0,4);return k((a-b)/864e5/7+1.5|0,2)},n:function(){return c.getMonth()+1},m:function(){return k(d.n(),2)},F:function(){return g[d.n()-1]},M:function(){return d.F().slice(0,3)},t:function(){return(new Date(d.Y(),d.n(),0)).getDate()},L:function(){return(new Date(d.Y(),1,29)).getMonth()===1|0},Y:function(){return c.getFullYear()},y:function(){return(d.Y()+"").slice(-2)},a:function(){return c.getHours()>11?"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()<b)},O:function(){var a=c.getTimezoneOffset();return(a>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<b)c="0"+c;return c}function i(a){return(a/10|0)===1?"th":a%10===1?"st":a%10===2?"nd":a%10===3?"rd":"th"}var d=this,e,f=["January","February","March","April","May","June","July","August","September","October","November","December"],g=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],h={future:"in %s",past:"%s ago",S:"less than a minute",m:"about a minute",M:"%d minutes",h:"about an hour",H:"about %d hours",d:"a day",D:"%d days",l:"about a month",L:"%d months",y:"about a year",Y:"%d years"};e={now:function(b){return b?(new a).getTime():new a},relativetime:function(a){var b=Math.abs(m(a))/1e3,c=b/60,d=c/60,e=d/24,f=e/365;return b<45&&n("S",b|0)||b<90&&n("m")||c<45&&n("M",c|0)||c<90&&n("h")||d<24&&n("H",d|0)||d<48&&n("d")||e<30&&n("D",e|0)||e<60&&n("l")||e<350&&n("L",e/30|0)||f<2&&n("y")||n("Y",f|0)},msapart:function(a,b){return m(a)-m(b)},fromnow:function(a,b){var c=e.msapart(a,b),d=c<0?h.past:h.future;return d.replace(/%s/i,e.relativetime(c))},customizedate:function(a){a.weekdays&&b.isArray(a.weekdays)&&a.weekdays.length===7&&(g=a.weekdays),a.months&&b.isArray(a.months)&&a.months.length===12&&(f=a.months),a.timeago&&b.extend(h,a.timeago),a.ordinal&&b.isFunction(a.ordinal)&&(i=a.ordinal)}},a.prototype.humanize=l,d._d=e,d._&&d._.mixin(e)})(Date,_)
module("Date");
test("dateFormat", function() {
- var dateTest = new Date(2010, 0, 3, 15, 25, 50, 125);
- expect(9);
- equal(_d.dateFormat('j d S', dateTest), "3 03 rd");
- equal(_d.dateFormat('l D w N', dateTest), "Sunday Sun 0 7");
- equal(_d.dateFormat('z W', dateTest), "2 53");
- equal(_d.dateFormat('n m F M t', dateTest), "1 01 January Jan 31");
- equal(_d.dateFormat('L Y y', dateTest), "0 2010 10");
- equal(_d.dateFormat('a A B', dateTest), "pm PM 017");
- equal(_d.dateFormat('g h G H', dateTest), "3 03 15 15");
- equal(_d.dateFormat('i s U', dateTest), "25 50 1262561150");
- equal(_d.dateFormat('I O P Z', dateTest), "0 -0800 -08:00 -28800");
+ var dateTest = new Date(2010, 1, 14, 15, 25, 50, 125);
+ expect(13);
+ equal(dateTest.humanize("w, l D1 Y, h:m2:s2 a"), "Sunday, February 14th 2010, 3:25:50 pm");
+ equal(dateTest.humanize("w1, hA"), "Sun, 3PM");
+ equal(dateTest.humanize("L L1 L2 L3 l l1"), "2 2nd 02 02nd February Feb");
+ equal(dateTest.humanize("Y Y1"), "2010 10");
+ equal(dateTest.humanize("D D1 D2 D3"), "14 14th 14 14th");
+ equal(dateTest.humanize("W W1 W2 W3 w w1"), "0 0th 00 00th Sunday Sun");
+ equal(dateTest.humanize("d d1 d2 d3"), "45 45th 045 045th");
+ equal(dateTest.humanize("K K1 K2 K3"), "8 8th 08 08th");
+ equal(dateTest.humanize("h h1 h2 h3"), "3 3rd 03 03rd");
+ equal(dateTest.humanize("H H1 H2 H3"), "15 15th 15 15th");
+ equal(dateTest.humanize("m m1 m2 m3"), "25 25th 25 25th");
+ equal(dateTest.humanize("s s1 s2 s3"), "50 50th 50 50th");
+ equal(dateTest.humanize("a A"), "pm PM");
});
});
\ No newline at end of file
(function() {
- var date1 = new Date(1000),
- date2 = new Date(1000);
-
- JSLitmus.test('_.timeFromNow(1000 * 30, 0)', function() {
- return _.timeFromNow(1000, 0);
- });
- JSLitmus.test('_.timeAsWords(1000 * 30)', function() {
- return _.timeAsWords(1000 * 30);
- });
+ var date1 = new Date(2010, 2, 6, 15, 25, 50, 125),
+ date2 = new Date(1000),
+ rt1 = 1000 * 60 * 60 * 24 * 365 * 5,
+ rt2 = 1000 * 30;
- JSLitmus.test('_.timeAsWords(1000 * 60 * 60 * 24 * 365 * 5)', function() {
- return _.timeAsWords(1000 * 60 * 60 * 24 * 365 * 5);
+ JSLitmus.test('Date.humanize => 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
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="date.js"></script>
- <script type="text/javascript" src="time.js"></script>
<script type="text/javascript" src="speed.js"></script>
+ <script type="text/javascript" src="time.js"></script>
</head>
<body>
<h1 id="qunit-header">underscore.date.js test suite</h1>
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");
});