From: Tim Wood Date: Tue, 8 Mar 2011 03:52:36 +0000 (-0800) Subject: Updated documentation to reflect change to YYYY-MM-DD format X-Git-Tag: 0.3.0~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a31b73bbc11564caa20d72ff090b8de19596cd9;p=thirdparty%2Fmoment.git Updated documentation to reflect change to YYYY-MM-DD format --- diff --git a/README.markdown b/README.markdown index 4233929b3..445bf6ebc 100644 --- a/README.markdown +++ b/README.markdown @@ -16,224 +16,169 @@ Date.prototype.humanize(format) Date.humanize returns a human readable string for a Date based on the format string that was passed in. var date = new Date(2010, 1, 14, 15, 25, 50, 125); - date.humanize("w, l D1 Y, h:m2:s2 a"); // "Sunday, February 14th 2010, 3:25:50 pm" - date.humanize("w1, hA"); // "Sun, 3PM" + date.humanize("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm" + date.humanize("ddd, hA"); // "Sun, 3PM" -The formats are created by creating a string of replacable characters with optional flags. - -For numerical outputs, appending 1 will add an ordinal, appending 2 will zero fill, and appending 3 will both zero fill and add an ordinal. - -For string outputs, appending 1 will return a 3 character shortening of the string. +The formats are created by creating a string of replacable characters. ### Month - - - - + - + + + + + + + + + + - -
InputLL1L2L3Output
OutputMonth
M 1 2 ... 11 12
Mo 1st 2nd ... 11th 12th
MM 01 02 ... 11 1201st 02nd ... 11th 12th
-The string month names use lowercase L - - - - + + - + - -
Inputll1MMMJan Feb ... Nov Dec
OutputMMMM January February ... November DecemberJan Feb ... Nov Dec
- -### Day of Month - - + + + - - - + - - + + + + - -
InputDay of Month
DD1D2D31 2 ... 30 30
Output1 2 ... 30 31Do 1st 2nd ... 30th 31st
DD 01 02 ... 30 3101st 02nd ... 30th 31st
- -### Day of Year - - - - - - + - + + + + + + + - -
InputDD1D2D3Day of Year
OutputDDD 1 2 ... 364 365
DDDo 1st 2nd ... 364th 365th
DDDD 001 002 ... 364 365001st 002nd ... 364th 365th
- -### Weekday - - - - - - + - - - - - + + -
InputWW1W2W3Day of Week
Output1 2 ... 6 71st 2nd ... 6th 7th1 2 ... 6 71st 2nd ... 6th 7thd0 1 ... 5 6
- - - - + + - - + -
Inputww1do0th 1st ... 5th 6th
OutputSunday Monday ... Friday Saturdayddd Sun Mon ... Fri Sat
- -### Week of Year - - - - - - + + - + + + + + + + + + + - -
InputKK1K2K3ddddSunday Monday ... Friday Saturday
OutputWeek of Year
w 1 2 ... 52 53
wo 1st 2nd ... 52nd 53rd
ww 01 02 ... 52 5301st 02nd ... 52nd 53rd
- -### Year -Returns the 4 digit year for `Y`, and the last 2 digits for `Y1`. - - - - + - - + -
InputYY1Year
Output1970 1971 ... 2029 2030YY 70 71 ... 29 30
- -### AM/PM -Returns uppercase `AM || PM` for uppercase `A` and lowercase `am || pm` for lowercase `a`. - - - - + + - + + + + + + + -
InputAaYYYY1970 1971 ... 2029 2030
OutputAM/PM
A AM PM
a am pm
- -### Hour -Returns 24 hour for uppercase `H` and 12 hour for lowercase `h`. - - - - - - - - - - + - + - + + + - + + + - + + + - -
InputHH1H2H3hh1h2h3Hour
OutputH 0 1 ... 22 230th 1st ... 22nd 23rd
HH 00 01 ... 22 2300th 01st ... 22nd 23rd
h 1 2 ... 11 121st 2nd ... 11th 12th
hh 01 02 ... 11 1201st 02nd ... 11th 12th
- -### Minute - - - - - - + - + - + + + - -
Inputmm1m2m3Minute
Outputm 0 1 ... 58 590th 1st ... 58th 59th
mm 00 01 ... 58 5900th 01st ... 58th 59th
- -### Second - - - - - - + - + - + + + -
Inputss1s2s3Second
Outputs 0 1 ... 58 590th 1st ... 58th 59th
ss 00 01 ... 58 5900th 01st ... 58th 59th
- Underscore mixin functions ========================== diff --git a/lib/underscore.date.js b/lib/underscore.date.js index 15f8d54c7..97371b33a 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -3,262 +3,259 @@ // (c) 2011 Tim Wood // Underscore.date is freely distributable under the terms of the MIT license. // -// Version 0.2.0 +// Version 0.3.0 /*global _:false */ (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", - s: "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", - M: "about a month", - MM: "%d months", - y: "about a year", - yy: "%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'; - } - - // left zero fill a number - function leftZeroFill(number, targetLength) { - var output = number + ''; - while (output.length < targetLength) { - output = '0' + output; - } - return 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 = /Mo|MM?M?M?|Do|DDDo|DD?D?D?|do|dd?d?d?|w[o|w]?|YYYY|YY|[aA]|hh?|HH?|mm?|ss?/g, - formatFunctions = { - // MONTH - M : function() { - return currentMonth + 1; - }, - Mo : function() { - return (currentMonth + 1) + createOrdinal(currentMonth + 1); - }, - MM : function() { - return leftZeroFill(currentMonth + 1, 2); - }, - MMM : function() { - return wordsMonths[currentMonth].slice(0, 3); - - }, - MMMM : function() { - return wordsMonths[currentMonth]; - - }, - - // DAY OF MONTH - D : function() { - return currentDate; - }, - Do : function() { - return currentDate + createOrdinal(currentDate); - }, - DD : function() { - return leftZeroFill(currentDate, 2); - }, - - // DAY OF YEAR - DDD : function() { - var a = new Date(currentYear, currentMonth, currentDate), - b = new Date(currentYear, 0, 1); - return ((a - b) / 864e5) + 1.5 | 0; - }, - DDDo : function() { - var DDD = formatFunctions.DDD(); - return DDD + createOrdinal(DDD); - }, - DDDD : function() { - return leftZeroFill(formatFunctions.DDD(), 3); - }, - - // WEEKDAY - d : function() { - return currentDay; - }, - do : function() { - return currentDay + createOrdinal(currentDay); - }, - dd : function() { - return leftZeroFill(currentDay, 2); - }, - ddd : function() { - return wordsWeekdays[currentDay].slice(0, 3); - }, - dddd : function() { - return wordsWeekdays[currentDay]; - }, - - // WEEK OF YEAR - w : function() { - var a = new Date(currentYear, currentMonth, currentDate - currentDay + 5), - b = new Date(a.getFullYear(), 0, 4); - return (a - b) / 864e5 / 7 + 1.5 | 0; - }, - wo : function() { - var w = formatFunctions.w(); - return w + createOrdinal(w); - }, - ww : function() { - return leftZeroFill(formatFunctions.w(), 2); - }, - - // YEAR - YY : function() { - return (currentYear + '').slice(-2); - }, - YYYY : function(shorthand) { - return currentYear; - }, - - // AM / PM - a : function() { - return currentHours > 11 ? 'pm' : 'am'; - }, - A : function() { - return currentHours > 11 ? 'PM' : 'AM'; - }, - - // 24 HOUR - H : function() { - return currentHours; - }, - HH : function() { - return leftZeroFill(currentHours, 2); - }, - - // 12 HOUR - h : function() { - return currentHours % 12 || 12; - }, - hh : function() { - return leftZeroFill(currentHours % 12 || 12, 2); - }, - - // MINUTE - m : function() { - return currentMinutes; - }, - mm : function() { - return leftZeroFill(currentMinutes, 2); - }, - - // SECOND - s : function() { - return currentSeconds; - }, - ss : function() { - return leftZeroFill(currentSeconds, 2); - } - }; - - // check if the character is a format - // return formatted string or non string. - function replaceFunction(input) { - return formatFunctions[input] ? formatFunctions[input]() : 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) { + 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", + s: "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", + M: "about a month", + MM: "%d months", + y: "about a year", + yy: "%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'; + } + + // left zero fill a number + function leftZeroFill(number, targetLength) { + var output = number + ''; + while (output.length < targetLength) { + output = '0' + output; + } + return 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 = /Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?/g, + formatFunctions = { + // MONTH + M : function() { + return currentMonth + 1; + }, + Mo : function() { + return (currentMonth + 1) + createOrdinal(currentMonth + 1); + }, + MM : function() { + return leftZeroFill(currentMonth + 1, 2); + }, + MMM : function() { + return wordsMonths[currentMonth].slice(0, 3); + + }, + MMMM : function() { + return wordsMonths[currentMonth]; + + }, + + // DAY OF MONTH + D : function() { + return currentDate; + }, + Do : function() { + return currentDate + createOrdinal(currentDate); + }, + DD : function() { + return leftZeroFill(currentDate, 2); + }, + + // DAY OF YEAR + DDD : function() { + var a = new Date(currentYear, currentMonth, currentDate), + b = new Date(currentYear, 0, 1); + return ((a - b) / 864e5) + 1.5 | 0; + }, + DDDo : function() { + var DDD = formatFunctions.DDD(); + return DDD + createOrdinal(DDD); + }, + DDDD : function() { + return leftZeroFill(formatFunctions.DDD(), 3); + }, + + // WEEKDAY + d : function() { + return currentDay; + }, + 'do' : function() { + return currentDay + createOrdinal(currentDay); + }, + ddd : function() { + return wordsWeekdays[currentDay].slice(0, 3); + }, + dddd : function() { + return wordsWeekdays[currentDay]; + }, + + // WEEK OF YEAR + w : function() { + var a = new Date(currentYear, currentMonth, currentDate - currentDay + 5), + b = new Date(a.getFullYear(), 0, 4); + return (a - b) / 864e5 / 7 + 1.5 | 0; + }, + wo : function() { + var w = formatFunctions.w(); + return w + createOrdinal(w); + }, + ww : function() { + return leftZeroFill(formatFunctions.w(), 2); + }, + + // YEAR + YY : function() { + return (currentYear + '').slice(-2); + }, + YYYY : function(shorthand) { + return currentYear; + }, + + // AM / PM + a : function() { + return currentHours > 11 ? 'pm' : 'am'; + }, + A : function() { + return currentHours > 11 ? 'PM' : 'AM'; + }, + + // 24 HOUR + H : function() { + return currentHours; + }, + HH : function() { + return leftZeroFill(currentHours, 2); + }, + + // 12 HOUR + h : function() { + return currentHours % 12 || 12; + }, + hh : function() { + return leftZeroFill(currentHours % 12 || 12, 2); + }, + + // MINUTE + m : function() { + return currentMinutes; + }, + mm : function() { + return leftZeroFill(currentMinutes, 2); + }, + + // SECOND + s : function() { + return currentSeconds; + }, + ss : function() { + return leftZeroFill(currentSeconds, 2); + } + }; + + // check if the character is a format + // return formatted string or non string. + function replaceFunction(input) { + return formatFunctions[input] ? formatFunctions[input]() : 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) { return wordsTimeAgo[string].replace(/%d/i, number || 1); } - - _d = { - now : function(asTimestamp) { - return asTimestamp ? new Date().getTime() : new Date(); - }, - 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('s', seconds | 0) || - seconds < 90 && substituteTimeAgo('m') || - minutes < 45 && substituteTimeAgo('mm', minutes | 0) || - minutes < 90 && substituteTimeAgo('h') || - hours < 24 && substituteTimeAgo('hh', hours | 0) || - hours < 48 && substituteTimeAgo('d') || - days < 30 && substituteTimeAgo('dd', days | 0) || - days < 60 && substituteTimeAgo('M') || - days < 350 && substituteTimeAgo('MM', days / 30 | 0) || - years < 2 && substituteTimeAgo('y') || - substituteTimeAgo('yy', years | 0); - }, - msapart : function(time, now) { - return makeInputMilliseconds(time) - makeInputMilliseconds(now); - }, - fromnow : function(time, now) { - var difference = _d.msapart(time, now), - string = difference < 0 ? wordsTimeAgo.past : wordsTimeAgo.future; - return string.replace(/%s/i, _d.relativetime(difference)); - }, - customizedate : function(input) { - if (input.weekdays && _.isArray(input.weekdays) && input.weekdays.length === 7) { - wordsWeekdays = input.weekdays; - } - if (input.months && _.isArray(input.months) && input.months.length === 12) { - wordsMonths = input.months; - } - if (input.timeago) { - _.extend(wordsTimeAgo, input.timeago); - } - if (input.ordinal && _.isFunction(input.ordinal)) { - createOrdinal = input.ordinal; - } - } - }; - - // assign to prototype - Date.prototype.humanize = humanize; - - // assign to global - self._d = _d; - - // integrate with underscore.js - if (self._) { - self._.mixin(_d); + + _d = { + now : function(asTimestamp) { + return asTimestamp ? new Date().getTime() : new Date(); + }, + 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('s', seconds | 0) || + seconds < 90 && substituteTimeAgo('m') || + minutes < 45 && substituteTimeAgo('mm', minutes | 0) || + minutes < 90 && substituteTimeAgo('h') || + hours < 24 && substituteTimeAgo('hh', hours | 0) || + hours < 48 && substituteTimeAgo('d') || + days < 30 && substituteTimeAgo('dd', days | 0) || + days < 60 && substituteTimeAgo('M') || + days < 350 && substituteTimeAgo('MM', days / 30 | 0) || + years < 2 && substituteTimeAgo('y') || + substituteTimeAgo('yy', years | 0); + }, + msapart : function(time, now) { + return makeInputMilliseconds(time) - makeInputMilliseconds(now); + }, + fromnow : function(time, now) { + var difference = _d.msapart(time, now), + string = difference < 0 ? wordsTimeAgo.past : wordsTimeAgo.future; + return string.replace(/%s/i, _d.relativetime(difference)); + }, + customizedate : function(input) { + if (input.weekdays && _.isArray(input.weekdays) && input.weekdays.length === 7) { + wordsWeekdays = input.weekdays; + } + if (input.months && _.isArray(input.months) && input.months.length === 12) { + wordsMonths = input.months; + } + if (input.timeago) { + _.extend(wordsTimeAgo, input.timeago); + } + if (input.ordinal && _.isFunction(input.ordinal)) { + createOrdinal = input.ordinal; + } + } + }; + + // assign to prototype + Date.prototype.humanize = humanize; + + // assign to global + self._d = _d; + + // integrate with underscore.js + if (_ && _.mixin) { + _.mixin(_d); } }(Date, _)); diff --git a/lib/underscore.date.min.js b/lib/underscore.date.min.js index e52acd3ea..617387d4c 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.2.0 +// Version 0.3.0 -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.length11?"pm":"am"},A:function(){return l>11?"PM":"AM"},H:function(){return l},HH:function(){return j(l,2)},h:function(){return l%12||12},hh:function(){return j(l%12||12,2)},m:function(){return m},mm:function(){return j(m,2)},s:function(){return n},ss:function(){return j(n,2)}};return b.replace(o,q)}function j(a,b){var c=a+"";while(c.length