From: Tim Wood Date: Mon, 14 Nov 2011 17:23:43 +0000 (-0800) Subject: Adding key to relative time callback function #60 X-Git-Tag: 1.1.2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02f13305de86eeda50e50dc412d231887b8e2ef4;p=thirdparty%2Fmoment.git Adding key to relative time callback function #60 --- diff --git a/moment.js b/moment.js index 85126e160..c4fc97939 100644 --- a/moment.js +++ b/moment.js @@ -389,26 +389,29 @@ // helper function for _date.from() and _date.fromNow() function substituteTimeAgo(string, number, withoutSuffix) { var rt = moment.relativeTime[string]; - return (typeof rt === 'function') ? rt(number || 1, !!withoutSuffix) : rt.replace(/%d/i, number || 1); + return (typeof rt === 'function') ? + rt(number || 1, !!withoutSuffix, string) : + rt.replace(/%d/i, number || 1); } function relativeTime(milliseconds, withoutSuffix) { - var seconds = Math.abs(milliseconds) / 1000, - minutes = seconds / 60, - hours = minutes / 60, - days = hours / 24, - years = days / 365; - return seconds < 45 && substituteTimeAgo('s', round(seconds), withoutSuffix) || - round(minutes) === 1 && substituteTimeAgo('m', 1, withoutSuffix) || - minutes < 45 && substituteTimeAgo('mm', round(minutes), withoutSuffix) || - round(hours) === 1 && substituteTimeAgo('h', 1, withoutSuffix) || - hours < 22 && substituteTimeAgo('hh', round(hours), withoutSuffix) || - round(days) === 1 && substituteTimeAgo('d', 1, withoutSuffix) || - days <= 25 && substituteTimeAgo('dd', round(days), withoutSuffix) || - days <= 45 && substituteTimeAgo('M', 1, withoutSuffix) || - days < 345 && substituteTimeAgo('MM', round(days / 30), withoutSuffix) || - round(years) === 1 && substituteTimeAgo('y', 1, withoutSuffix) || - substituteTimeAgo('yy', round(years), withoutSuffix); + var seconds = round(Math.abs(milliseconds) / 1000), + minutes = round(seconds / 60), + hours = round(minutes / 60), + days = round(hours / 24), + years = round(days / 365), + args = seconds < 45 && ['s', seconds] || + minutes === 1 && ['m'] || + minutes < 45 && ['mm', minutes] || + hours === 1 && ['h'] || + hours < 22 && ['hh', hours] || + days === 1 && ['d'] || + days <= 25 && ['dd', days] || + days <= 45 && ['M'] || + days < 345 && ['MM', round(days / 30)] || + years === 1 && ['y'] || ['yy', years]; + args[2] = withoutSuffix; + return substituteTimeAgo.apply({}, args); } // shortcut for prototype diff --git a/moment.min.js b/moment.min.js index 5014afb03..c9018a792 100755 --- a/moment.min.js +++ b/moment.min.js @@ -1,2 +1,2 @@ /* Moment.js | version : 1.1.1 | author : Tim Wood | license : MIT */ -(function(a,b){function k(a,b){var c=a+"";while(c.length11?"pm":"am";case"A":return i>11?"PM":"AM";case"H":return i;case"HH":return k(i,2);case"h":return i%12||12;case"hh":return k(i%12||12,2);case"m":return j;case"mm":return k(j,2);case"s":return l;case"ss":return k(l,2);case"zz":case"z":return(b.toString().match(p)||[""])[0].replace(n,"");case"L":case"LL":case"LLL":case"LLLL":return o(b,c.longDateFormat[d]);default:return d.replace("\\","")}}var e=b.getMonth(),f=b.getDate(),g=b.getFullYear(),h=b.getDay(),i=b.getHours(),j=b.getMinutes(),l=b.getSeconds(),m=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?|LL?L?L?)/g,n=/[^A-Z]/g,p=/\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g,q=c.ordinal;return d.replace(m,r)}function p(a,b){function j(a,b){switch(a){case"M":case"MM":c[1]=~~b-1;break;case"D":case"DD":case"DDD":case"DDDD":c[2]=~~b;break;case"YY":b=~~b,c[0]=b+(b>70?1900:2e3);break;case"YYYY":c[0]=~~b;break;case"a":case"A":i=b.toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":c[3]=~~b;break;case"m":case"mm":c[4]=~~b;break;case"s":case"ss":c[5]=~~b}}var c=[0],d=/(\\)?(MM?|DD?D?D?|YYYY|YY|a|A|hh?|HH?|mm?|ss?)/g,e=/(\\)?([0-9]+|am|pm)/gi,f=a.match(e),g=b.match(d),h,i;for(h=0;h11?"pm":"am";case"A":return i>11?"PM":"AM";case"H":return i;case"HH":return k(i,2);case"h":return i%12||12;case"hh":return k(i%12||12,2);case"m":return j;case"mm":return k(j,2);case"s":return l;case"ss":return k(l,2);case"zz":case"z":return(b.toString().match(p)||[""])[0].replace(n,"");case"L":case"LL":case"LLL":case"LLLL":return o(b,c.longDateFormat[d]);default:return d.replace("\\","")}}var e=b.getMonth(),f=b.getDate(),g=b.getFullYear(),h=b.getDay(),i=b.getHours(),j=b.getMinutes(),l=b.getSeconds(),m=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?|LL?L?L?)/g,n=/[^A-Z]/g,p=/\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g,q=c.ordinal;return d.replace(m,r)}function p(a,b){function j(a,b){switch(a){case"M":case"MM":c[1]=~~b-1;break;case"D":case"DD":case"DDD":case"DDDD":c[2]=~~b;break;case"YY":b=~~b,c[0]=b+(b>70?1900:2e3);break;case"YYYY":c[0]=~~b;break;case"a":case"A":i=b.toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":c[3]=~~b;break;case"m":case"mm":c[4]=~~b;break;case"s":case"ss":c[5]=~~b}}var c=[0],d=/(\\)?(MM?|DD?D?D?|YYYY|YY|a|A|hh?|HH?|mm?|ss?)/g,e=/(\\)?([0-9]+|am|pm)/gi,f=a.match(e),g=b.match(d),h,i;for(h=0;hMoment.js Documentation

Moment.js Documentation

A lightweight javascript date library for parsing, manipulating, and formatting dates.

Where to get it

Github

Production Version 1.1.12.5kb minified & gzippedDevelopment Version 1.1.118.1kb full source + comments

You can also clone the project with Git by running:

git clone git://github.com/timrwood/moment

npm

npm install moment

Where to use it

Moment was designed to work in both the browser and in NodeJS. All code will work in both environments. All unit tests are run in both environments.

In NodeJS

var moment = require('moment');
+Moment.js Documentation

Moment.js Documentation

A lightweight javascript date library for parsing, manipulating, and formatting dates.

Where to get it

Github

Production Version 1.1.12.5kb minified & gzippedDevelopment Version 1.1.117.9kb full source + comments

You can also clone the project with Git by running:

git clone git://github.com/timrwood/moment

npm

npm install moment

Where to use it

Moment was designed to work in both the browser and in NodeJS. All code will work in both environments. All unit tests are run in both environments.

In NodeJS

var moment = require('moment');
 moment().add('hours', 1).fromNow(); // "1 hour ago"
 

In the browser

<script src="moment.min.js"></script>
 moment().add('hours', 1).fromNow(); // "1 hour ago"
@@ -181,7 +181,7 @@ moment(1316116057189).fromNow(); // il y a une heure
         (b === 2) ? 'nd' : 
         (b === 3) ? 'rd' : 'th';
 };
-

For more information on ordinal numbers, see wikipedia