moment.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. // moment.js
  2. // version : 2.1.0
  3. // author : Tim Wood
  4. // license : MIT
  5. // momentjs.com
  6. (function (undefined) {
  7. /************************************
  8. Constants
  9. ************************************/
  10. var moment,
  11. VERSION = "2.1.0",
  12. round = Math.round, i,
  13. // internal storage for language config files
  14. languages = {},
  15. // check for nodeJS
  16. hasModule = (typeof module !== 'undefined' && module.exports),
  17. // ASP.NET json date format regex
  18. aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
  19. aspNetTimeSpanJsonRegex = /(\-)?(\d*)?\.?(\d+)\:(\d+)\:(\d+)\.?(\d{3})?/,
  20. // format tokens
  21. formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g,
  22. localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
  23. // parsing token regexes
  24. parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99
  25. parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999
  26. parseTokenThreeDigits = /\d{3}/, // 000 - 999
  27. parseTokenFourDigits = /\d{1,4}/, // 0 - 9999
  28. parseTokenSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999
  29. parseTokenWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i, // any word (or two) characters or numbers including two/three word month in arabic.
  30. parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, // +00:00 -00:00 +0000 -0000 or Z
  31. parseTokenT = /T/i, // T (ISO seperator)
  32. parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
  33. // preliminary iso regex
  34. // 0000-00-00 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000
  35. isoRegex = /^\s*\d{4}-\d\d-\d\d((T| )(\d\d(:\d\d(:\d\d(\.\d\d?\d?)?)?)?)?([\+\-]\d\d:?\d\d)?)?/,
  36. isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
  37. // iso time formats and regexes
  38. isoTimes = [
  39. ['HH:mm:ss.S', /(T| )\d\d:\d\d:\d\d\.\d{1,3}/],
  40. ['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/],
  41. ['HH:mm', /(T| )\d\d:\d\d/],
  42. ['HH', /(T| )\d\d/]
  43. ],
  44. // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
  45. parseTimezoneChunker = /([\+\-]|\d\d)/gi,
  46. // getter and setter names
  47. proxyGettersAndSetters = 'Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
  48. unitMillisecondFactors = {
  49. 'Milliseconds' : 1,
  50. 'Seconds' : 1e3,
  51. 'Minutes' : 6e4,
  52. 'Hours' : 36e5,
  53. 'Days' : 864e5,
  54. 'Months' : 2592e6,
  55. 'Years' : 31536e6
  56. },
  57. unitAliases = {
  58. ms : 'millisecond',
  59. s : 'second',
  60. m : 'minute',
  61. h : 'hour',
  62. d : 'day',
  63. w : 'week',
  64. M : 'month',
  65. y : 'year'
  66. },
  67. // format function strings
  68. formatFunctions = {},
  69. // tokens to ordinalize and pad
  70. ordinalizeTokens = 'DDD w W M D d'.split(' '),
  71. paddedTokens = 'M D H h m s w W'.split(' '),
  72. formatTokenFunctions = {
  73. M : function () {
  74. return this.month() + 1;
  75. },
  76. MMM : function (format) {
  77. return this.lang().monthsShort(this, format);
  78. },
  79. MMMM : function (format) {
  80. return this.lang().months(this, format);
  81. },
  82. D : function () {
  83. return this.date();
  84. },
  85. DDD : function () {
  86. return this.dayOfYear();
  87. },
  88. d : function () {
  89. return this.day();
  90. },
  91. dd : function (format) {
  92. return this.lang().weekdaysMin(this, format);
  93. },
  94. ddd : function (format) {
  95. return this.lang().weekdaysShort(this, format);
  96. },
  97. dddd : function (format) {
  98. return this.lang().weekdays(this, format);
  99. },
  100. w : function () {
  101. return this.week();
  102. },
  103. W : function () {
  104. return this.isoWeek();
  105. },
  106. YY : function () {
  107. return leftZeroFill(this.year() % 100, 2);
  108. },
  109. YYYY : function () {
  110. return leftZeroFill(this.year(), 4);
  111. },
  112. YYYYY : function () {
  113. return leftZeroFill(this.year(), 5);
  114. },
  115. gg : function () {
  116. return leftZeroFill(this.weekYear() % 100, 2);
  117. },
  118. gggg : function () {
  119. return this.weekYear();
  120. },
  121. ggggg : function () {
  122. return leftZeroFill(this.weekYear(), 5);
  123. },
  124. GG : function () {
  125. return leftZeroFill(this.isoWeekYear() % 100, 2);
  126. },
  127. GGGG : function () {
  128. return this.isoWeekYear();
  129. },
  130. GGGGG : function () {
  131. return leftZeroFill(this.isoWeekYear(), 5);
  132. },
  133. e : function () {
  134. return this.weekday();
  135. },
  136. E : function () {
  137. return this.isoWeekday();
  138. },
  139. a : function () {
  140. return this.lang().meridiem(this.hours(), this.minutes(), true);
  141. },
  142. A : function () {
  143. return this.lang().meridiem(this.hours(), this.minutes(), false);
  144. },
  145. H : function () {
  146. return this.hours();
  147. },
  148. h : function () {
  149. return this.hours() % 12 || 12;
  150. },
  151. m : function () {
  152. return this.minutes();
  153. },
  154. s : function () {
  155. return this.seconds();
  156. },
  157. S : function () {
  158. return ~~(this.milliseconds() / 100);
  159. },
  160. SS : function () {
  161. return leftZeroFill(~~(this.milliseconds() / 10), 2);
  162. },
  163. SSS : function () {
  164. return leftZeroFill(this.milliseconds(), 3);
  165. },
  166. Z : function () {
  167. var a = -this.zone(),
  168. b = "+";
  169. if (a < 0) {
  170. a = -a;
  171. b = "-";
  172. }
  173. return b + leftZeroFill(~~(a / 60), 2) + ":" + leftZeroFill(~~a % 60, 2);
  174. },
  175. ZZ : function () {
  176. var a = -this.zone(),
  177. b = "+";
  178. if (a < 0) {
  179. a = -a;
  180. b = "-";
  181. }
  182. return b + leftZeroFill(~~(10 * a / 6), 4);
  183. },
  184. z : function () {
  185. return this.zoneAbbr();
  186. },
  187. zz : function () {
  188. return this.zoneName();
  189. },
  190. X : function () {
  191. return this.unix();
  192. }
  193. };
  194. function padToken(func, count) {
  195. return function (a) {
  196. return leftZeroFill(func.call(this, a), count);
  197. };
  198. }
  199. function ordinalizeToken(func, period) {
  200. return function (a) {
  201. return this.lang().ordinal(func.call(this, a), period);
  202. };
  203. }
  204. while (ordinalizeTokens.length) {
  205. i = ordinalizeTokens.pop();
  206. formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i], i);
  207. }
  208. while (paddedTokens.length) {
  209. i = paddedTokens.pop();
  210. formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2);
  211. }
  212. formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
  213. /************************************
  214. Constructors
  215. ************************************/
  216. function Language() {
  217. }
  218. // Moment prototype object
  219. function Moment(config) {
  220. extend(this, config);
  221. }
  222. // Duration Constructor
  223. function Duration(duration) {
  224. var years = duration.years || duration.year || duration.y || 0,
  225. months = duration.months || duration.month || duration.M || 0,
  226. weeks = duration.weeks || duration.week || duration.w || 0,
  227. days = duration.days || duration.day || duration.d || 0,
  228. hours = duration.hours || duration.hour || duration.h || 0,
  229. minutes = duration.minutes || duration.minute || duration.m || 0,
  230. seconds = duration.seconds || duration.second || duration.s || 0,
  231. milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0;
  232. // store reference to input for deterministic cloning
  233. this._input = duration;
  234. // representation for dateAddRemove
  235. this._milliseconds = milliseconds +
  236. seconds * 1e3 + // 1000
  237. minutes * 6e4 + // 1000 * 60
  238. hours * 36e5; // 1000 * 60 * 60
  239. // Because of dateAddRemove treats 24 hours as different from a
  240. // day when working around DST, we need to store them separately
  241. this._days = days +
  242. weeks * 7;
  243. // It is impossible translate months into days without knowing
  244. // which months you are are talking about, so we have to store
  245. // it separately.
  246. this._months = months +
  247. years * 12;
  248. this._data = {};
  249. this._bubble();
  250. }
  251. /************************************
  252. Helpers
  253. ************************************/
  254. function extend(a, b) {
  255. for (var i in b) {
  256. if (b.hasOwnProperty(i)) {
  257. a[i] = b[i];
  258. }
  259. }
  260. return a;
  261. }
  262. function absRound(number) {
  263. if (number < 0) {
  264. return Math.ceil(number);
  265. } else {
  266. return Math.floor(number);
  267. }
  268. }
  269. // left zero fill a number
  270. // see http://jsperf.com/left-zero-filling for performance comparison
  271. function leftZeroFill(number, targetLength) {
  272. var output = number + '';
  273. while (output.length < targetLength) {
  274. output = '0' + output;
  275. }
  276. return output;
  277. }
  278. // helper function for _.addTime and _.subtractTime
  279. function addOrSubtractDurationFromMoment(mom, duration, isAdding, ignoreUpdateOffset) {
  280. var milliseconds = duration._milliseconds,
  281. days = duration._days,
  282. months = duration._months,
  283. minutes,
  284. hours,
  285. currentDate;
  286. if (milliseconds) {
  287. mom._d.setTime(+mom._d + milliseconds * isAdding);
  288. }
  289. // store the minutes and hours so we can restore them
  290. if (days || months) {
  291. minutes = mom.minute();
  292. hours = mom.hour();
  293. }
  294. if (days) {
  295. mom.date(mom.date() + days * isAdding);
  296. }
  297. if (months) {
  298. mom.month(mom.month() + months * isAdding);
  299. }
  300. if (milliseconds && !ignoreUpdateOffset) {
  301. moment.updateOffset(mom);
  302. }
  303. // restore the minutes and hours after possibly changing dst
  304. if (days || months) {
  305. mom.minute(minutes);
  306. mom.hour(hours);
  307. }
  308. }
  309. // check if is an array
  310. function isArray(input) {
  311. return Object.prototype.toString.call(input) === '[object Array]';
  312. }
  313. // compare two arrays, return the number of differences
  314. function compareArrays(array1, array2) {
  315. var len = Math.min(array1.length, array2.length),
  316. lengthDiff = Math.abs(array1.length - array2.length),
  317. diffs = 0,
  318. i;
  319. for (i = 0; i < len; i++) {
  320. if (~~array1[i] !== ~~array2[i]) {
  321. diffs++;
  322. }
  323. }
  324. return diffs + lengthDiff;
  325. }
  326. function normalizeUnits(units) {
  327. return units ? unitAliases[units] || units.toLowerCase().replace(/(.)s$/, '$1') : units;
  328. }
  329. /************************************
  330. Languages
  331. ************************************/
  332. Language.prototype = {
  333. set : function (config) {
  334. var prop, i;
  335. for (i in config) {
  336. prop = config[i];
  337. if (typeof prop === 'function') {
  338. this[i] = prop;
  339. } else {
  340. this['_' + i] = prop;
  341. }
  342. }
  343. },
  344. _months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
  345. months : function (m) {
  346. return this._months[m.month()];
  347. },
  348. _monthsShort : "Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),
  349. monthsShort : function (m) {
  350. return this._monthsShort[m.month()];
  351. },
  352. monthsParse : function (monthName) {
  353. var i, mom, regex;
  354. if (!this._monthsParse) {
  355. this._monthsParse = [];
  356. }
  357. for (i = 0; i < 12; i++) {
  358. // make the regex if we don't have it already
  359. if (!this._monthsParse[i]) {
  360. mom = moment([2000, i]);
  361. regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
  362. this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
  363. }
  364. // test the regex
  365. if (this._monthsParse[i].test(monthName)) {
  366. return i;
  367. }
  368. }
  369. },
  370. _weekdays : "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
  371. weekdays : function (m) {
  372. return this._weekdays[m.day()];
  373. },
  374. _weekdaysShort : "Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),
  375. weekdaysShort : function (m) {
  376. return this._weekdaysShort[m.day()];
  377. },
  378. _weekdaysMin : "Su_Mo_Tu_We_Th_Fr_Sa".split("_"),
  379. weekdaysMin : function (m) {
  380. return this._weekdaysMin[m.day()];
  381. },
  382. weekdaysParse : function (weekdayName) {
  383. var i, mom, regex;
  384. if (!this._weekdaysParse) {
  385. this._weekdaysParse = [];
  386. }
  387. for (i = 0; i < 7; i++) {
  388. // make the regex if we don't have it already
  389. if (!this._weekdaysParse[i]) {
  390. mom = moment([2000, 1]).day(i);
  391. regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
  392. this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i');
  393. }
  394. // test the regex
  395. if (this._weekdaysParse[i].test(weekdayName)) {
  396. return i;
  397. }
  398. }
  399. },
  400. _longDateFormat : {
  401. LT : "h:mm A",
  402. L : "MM/DD/YYYY",
  403. LL : "MMMM D YYYY",
  404. LLL : "MMMM D YYYY LT",
  405. LLLL : "dddd, MMMM D YYYY LT"
  406. },
  407. longDateFormat : function (key) {
  408. var output = this._longDateFormat[key];
  409. if (!output && this._longDateFormat[key.toUpperCase()]) {
  410. output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
  411. return val.slice(1);
  412. });
  413. this._longDateFormat[key] = output;
  414. }
  415. return output;
  416. },
  417. isPM : function (input) {
  418. return ((input + '').toLowerCase()[0] === 'p');
  419. },
  420. _meridiemParse : /[ap]\.?m?\.?/i,
  421. meridiem : function (hours, minutes, isLower) {
  422. if (hours > 11) {
  423. return isLower ? 'pm' : 'PM';
  424. } else {
  425. return isLower ? 'am' : 'AM';
  426. }
  427. },
  428. _calendar : {
  429. sameDay : '[Today at] LT',
  430. nextDay : '[Tomorrow at] LT',
  431. nextWeek : 'dddd [at] LT',
  432. lastDay : '[Yesterday at] LT',
  433. lastWeek : '[Last] dddd [at] LT',
  434. sameElse : 'L'
  435. },
  436. calendar : function (key, mom) {
  437. var output = this._calendar[key];
  438. return typeof output === 'function' ? output.apply(mom) : output;
  439. },
  440. _relativeTime : {
  441. future : "in %s",
  442. past : "%s ago",
  443. s : "a few seconds",
  444. m : "a minute",
  445. mm : "%d minutes",
  446. h : "an hour",
  447. hh : "%d hours",
  448. d : "a day",
  449. dd : "%d days",
  450. M : "a month",
  451. MM : "%d months",
  452. y : "a year",
  453. yy : "%d years"
  454. },
  455. relativeTime : function (number, withoutSuffix, string, isFuture) {
  456. var output = this._relativeTime[string];
  457. return (typeof output === 'function') ?
  458. output(number, withoutSuffix, string, isFuture) :
  459. output.replace(/%d/i, number);
  460. },
  461. pastFuture : function (diff, output) {
  462. var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
  463. return typeof format === 'function' ? format(output) : format.replace(/%s/i, output);
  464. },
  465. ordinal : function (number) {
  466. return this._ordinal.replace("%d", number);
  467. },
  468. _ordinal : "%d",
  469. preparse : function (string) {
  470. return string;
  471. },
  472. postformat : function (string) {
  473. return string;
  474. },
  475. week : function (mom) {
  476. return weekOfYear(mom, this._week.dow, this._week.doy).week;
  477. },
  478. _week : {
  479. dow : 0, // Sunday is the first day of the week.
  480. doy : 6 // The week that contains Jan 1st is the first week of the year.
  481. }
  482. };
  483. // Loads a language definition into the `languages` cache. The function
  484. // takes a key and optionally values. If not in the browser and no values
  485. // are provided, it will load the language file module. As a convenience,
  486. // this function also returns the language values.
  487. function loadLang(key, values) {
  488. values.abbr = key;
  489. if (!languages[key]) {
  490. languages[key] = new Language();
  491. }
  492. languages[key].set(values);
  493. return languages[key];
  494. }
  495. // Determines which language definition to use and returns it.
  496. //
  497. // With no parameters, it will return the global language. If you
  498. // pass in a language key, such as 'en', it will return the
  499. // definition for 'en', so long as 'en' has already been loaded using
  500. // moment.lang.
  501. function getLangDefinition(key) {
  502. if (!key) {
  503. return moment.fn._lang;
  504. }
  505. if (!languages[key] && hasModule) {
  506. try {
  507. require('./lang/' + key);
  508. } catch (e) {
  509. // call with no params to set to default
  510. return moment.fn._lang;
  511. }
  512. }
  513. return languages[key];
  514. }
  515. /************************************
  516. Formatting
  517. ************************************/
  518. function removeFormattingTokens(input) {
  519. if (input.match(/\[.*\]/)) {
  520. return input.replace(/^\[|\]$/g, "");
  521. }
  522. return input.replace(/\\/g, "");
  523. }
  524. function makeFormatFunction(format) {
  525. var array = format.match(formattingTokens), i, length;
  526. for (i = 0, length = array.length; i < length; i++) {
  527. if (formatTokenFunctions[array[i]]) {
  528. array[i] = formatTokenFunctions[array[i]];
  529. } else {
  530. array[i] = removeFormattingTokens(array[i]);
  531. }
  532. }
  533. return function (mom) {
  534. var output = "";
  535. for (i = 0; i < length; i++) {
  536. output += array[i] instanceof Function ? array[i].call(mom, format) : array[i];
  537. }
  538. return output;
  539. };
  540. }
  541. // format date using native date object
  542. function formatMoment(m, format) {
  543. var i = 5;
  544. function replaceLongDateFormatTokens(input) {
  545. return m.lang().longDateFormat(input) || input;
  546. }
  547. while (i-- && localFormattingTokens.test(format)) {
  548. format = format.replace(localFormattingTokens, replaceLongDateFormatTokens);
  549. }
  550. if (!formatFunctions[format]) {
  551. formatFunctions[format] = makeFormatFunction(format);
  552. }
  553. return formatFunctions[format](m);
  554. }
  555. /************************************
  556. Parsing
  557. ************************************/
  558. // get the regex to find the next token
  559. function getParseRegexForToken(token, config) {
  560. switch (token) {
  561. case 'DDDD':
  562. return parseTokenThreeDigits;
  563. case 'YYYY':
  564. return parseTokenFourDigits;
  565. case 'YYYYY':
  566. return parseTokenSixDigits;
  567. case 'S':
  568. case 'SS':
  569. case 'SSS':
  570. case 'DDD':
  571. return parseTokenOneToThreeDigits;
  572. case 'MMM':
  573. case 'MMMM':
  574. case 'dd':
  575. case 'ddd':
  576. case 'dddd':
  577. return parseTokenWord;
  578. case 'a':
  579. case 'A':
  580. return getLangDefinition(config._l)._meridiemParse;
  581. case 'X':
  582. return parseTokenTimestampMs;
  583. case 'Z':
  584. case 'ZZ':
  585. return parseTokenTimezone;
  586. case 'T':
  587. return parseTokenT;
  588. case 'MM':
  589. case 'DD':
  590. case 'YY':
  591. case 'HH':
  592. case 'hh':
  593. case 'mm':
  594. case 'ss':
  595. case 'M':
  596. case 'D':
  597. case 'd':
  598. case 'H':
  599. case 'h':
  600. case 'm':
  601. case 's':
  602. return parseTokenOneOrTwoDigits;
  603. default :
  604. return new RegExp(token.replace('\\', ''));
  605. }
  606. }
  607. function timezoneMinutesFromString(string) {
  608. var tzchunk = (parseTokenTimezone.exec(string) || [])[0],
  609. parts = (tzchunk + '').match(parseTimezoneChunker) || ['-', 0, 0],
  610. minutes = +(parts[1] * 60) + ~~parts[2];
  611. return parts[0] === '+' ? -minutes : minutes;
  612. }
  613. // function to convert string input to date
  614. function addTimeToArrayFromToken(token, input, config) {
  615. var a, datePartArray = config._a;
  616. switch (token) {
  617. // MONTH
  618. case 'M' : // fall through to MM
  619. case 'MM' :
  620. datePartArray[1] = (input == null) ? 0 : ~~input - 1;
  621. break;
  622. case 'MMM' : // fall through to MMMM
  623. case 'MMMM' :
  624. a = getLangDefinition(config._l).monthsParse(input);
  625. // if we didn't find a month name, mark the date as invalid.
  626. if (a != null) {
  627. datePartArray[1] = a;
  628. } else {
  629. config._isValid = false;
  630. }
  631. break;
  632. // DAY OF MONTH
  633. case 'D' : // fall through to DDDD
  634. case 'DD' : // fall through to DDDD
  635. case 'DDD' : // fall through to DDDD
  636. case 'DDDD' :
  637. if (input != null) {
  638. datePartArray[2] = ~~input;
  639. }
  640. break;
  641. // YEAR
  642. case 'YY' :
  643. datePartArray[0] = ~~input + (~~input > 68 ? 1900 : 2000);
  644. break;
  645. case 'YYYY' :
  646. case 'YYYYY' :
  647. datePartArray[0] = ~~input;
  648. break;
  649. // AM / PM
  650. case 'a' : // fall through to A
  651. case 'A' :
  652. config._isPm = getLangDefinition(config._l).isPM(input);
  653. break;
  654. // 24 HOUR
  655. case 'H' : // fall through to hh
  656. case 'HH' : // fall through to hh
  657. case 'h' : // fall through to hh
  658. case 'hh' :
  659. datePartArray[3] = ~~input;
  660. break;
  661. // MINUTE
  662. case 'm' : // fall through to mm
  663. case 'mm' :
  664. datePartArray[4] = ~~input;
  665. break;
  666. // SECOND
  667. case 's' : // fall through to ss
  668. case 'ss' :
  669. datePartArray[5] = ~~input;
  670. break;
  671. // MILLISECOND
  672. case 'S' :
  673. case 'SS' :
  674. case 'SSS' :
  675. datePartArray[6] = ~~ (('0.' + input) * 1000);
  676. break;
  677. // UNIX TIMESTAMP WITH MS
  678. case 'X':
  679. config._d = new Date(parseFloat(input) * 1000);
  680. break;
  681. // TIMEZONE
  682. case 'Z' : // fall through to ZZ
  683. case 'ZZ' :
  684. config._useUTC = true;
  685. config._tzm = timezoneMinutesFromString(input);
  686. break;
  687. }
  688. // if the input is null, the date is not valid
  689. if (input == null) {
  690. config._isValid = false;
  691. }
  692. }
  693. // convert an array to a date.
  694. // the array should mirror the parameters below
  695. // note: all values past the year are optional and will default to the lowest possible value.
  696. // [year, month, day , hour, minute, second, millisecond]
  697. function dateFromArray(config) {
  698. var i, date, input = [];
  699. if (config._d) {
  700. return;
  701. }
  702. for (i = 0; i < 7; i++) {
  703. config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
  704. }
  705. // add the offsets to the time to be parsed so that we can have a clean array for checking isValid
  706. input[3] += ~~((config._tzm || 0) / 60);
  707. input[4] += ~~((config._tzm || 0) % 60);
  708. date = new Date(0);
  709. if (config._useUTC) {
  710. date.setUTCFullYear(input[0], input[1], input[2]);
  711. date.setUTCHours(input[3], input[4], input[5], input[6]);
  712. } else {
  713. date.setFullYear(input[0], input[1], input[2]);
  714. date.setHours(input[3], input[4], input[5], input[6]);
  715. }
  716. config._d = date;
  717. }
  718. // date from string and format string
  719. function makeDateFromStringAndFormat(config) {
  720. // This array is used to make a Date, either with `new Date` or `Date.UTC`
  721. var tokens = config._f.match(formattingTokens),
  722. string = config._i,
  723. i, parsedInput;
  724. config._a = [];
  725. for (i = 0; i < tokens.length; i++) {
  726. parsedInput = (getParseRegexForToken(tokens[i], config).exec(string) || [])[0];
  727. if (parsedInput) {
  728. string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
  729. }
  730. // don't parse if its not a known token
  731. if (formatTokenFunctions[tokens[i]]) {
  732. addTimeToArrayFromToken(tokens[i], parsedInput, config);
  733. }
  734. }
  735. // add remaining unparsed input to the string
  736. if (string) {
  737. config._il = string;
  738. }
  739. // handle am pm
  740. if (config._isPm && config._a[3] < 12) {
  741. config._a[3] += 12;
  742. }
  743. // if is 12 am, change hours to 0
  744. if (config._isPm === false && config._a[3] === 12) {
  745. config._a[3] = 0;
  746. }
  747. // return
  748. dateFromArray(config);
  749. }
  750. // date from string and array of format strings
  751. function makeDateFromStringAndArray(config) {
  752. var tempConfig,
  753. tempMoment,
  754. bestMoment,
  755. scoreToBeat = 99,
  756. i,
  757. currentScore;
  758. for (i = 0; i < config._f.length; i++) {
  759. tempConfig = extend({}, config);
  760. tempConfig._f = config._f[i];
  761. makeDateFromStringAndFormat(tempConfig);
  762. tempMoment = new Moment(tempConfig);
  763. currentScore = compareArrays(tempConfig._a, tempMoment.toArray());
  764. // if there is any input that was not parsed
  765. // add a penalty for that format
  766. if (tempMoment._il) {
  767. currentScore += tempMoment._il.length;
  768. }
  769. if (currentScore < scoreToBeat) {
  770. scoreToBeat = currentScore;
  771. bestMoment = tempMoment;
  772. }
  773. }
  774. extend(config, bestMoment);
  775. }
  776. // date from iso format
  777. function makeDateFromString(config) {
  778. var i,
  779. string = config._i,
  780. match = isoRegex.exec(string);
  781. if (match) {
  782. // match[2] should be "T" or undefined
  783. config._f = 'YYYY-MM-DD' + (match[2] || " ");
  784. for (i = 0; i < 4; i++) {
  785. if (isoTimes[i][1].exec(string)) {
  786. config._f += isoTimes[i][0];
  787. break;
  788. }
  789. }
  790. if (parseTokenTimezone.exec(string)) {
  791. config._f += " Z";
  792. }
  793. makeDateFromStringAndFormat(config);
  794. } else {
  795. config._d = new Date(string);
  796. }
  797. }
  798. function makeDateFromInput(config) {
  799. var input = config._i,
  800. matched = aspNetJsonRegex.exec(input);
  801. if (input === undefined) {
  802. config._d = new Date();
  803. } else if (matched) {
  804. config._d = new Date(+matched[1]);
  805. } else if (typeof input === 'string') {
  806. makeDateFromString(config);
  807. } else if (isArray(input)) {
  808. config._a = input.slice(0);
  809. dateFromArray(config);
  810. } else {
  811. config._d = input instanceof Date ? new Date(+input) : new Date(input);
  812. }
  813. }
  814. /************************************
  815. Relative Time
  816. ************************************/
  817. // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
  818. function substituteTimeAgo(string, number, withoutSuffix, isFuture, lang) {
  819. return lang.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
  820. }
  821. function relativeTime(milliseconds, withoutSuffix, lang) {
  822. var seconds = round(Math.abs(milliseconds) / 1000),
  823. minutes = round(seconds / 60),
  824. hours = round(minutes / 60),
  825. days = round(hours / 24),
  826. years = round(days / 365),
  827. args = seconds < 45 && ['s', seconds] ||
  828. minutes === 1 && ['m'] ||
  829. minutes < 45 && ['mm', minutes] ||
  830. hours === 1 && ['h'] ||
  831. hours < 22 && ['hh', hours] ||
  832. days === 1 && ['d'] ||
  833. days <= 25 && ['dd', days] ||
  834. days <= 45 && ['M'] ||
  835. days < 345 && ['MM', round(days / 30)] ||
  836. years === 1 && ['y'] || ['yy', years];
  837. args[2] = withoutSuffix;
  838. args[3] = milliseconds > 0;
  839. args[4] = lang;
  840. return substituteTimeAgo.apply({}, args);
  841. }
  842. /************************************
  843. Week of Year
  844. ************************************/
  845. // firstDayOfWeek 0 = sun, 6 = sat
  846. // the day of the week that starts the week
  847. // (usually sunday or monday)
  848. // firstDayOfWeekOfYear 0 = sun, 6 = sat
  849. // the first week is the week that contains the first
  850. // of this day of the week
  851. // (eg. ISO weeks use thursday (4))
  852. function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
  853. var end = firstDayOfWeekOfYear - firstDayOfWeek,
  854. daysToDayOfWeek = firstDayOfWeekOfYear - mom.day(),
  855. adjustedMoment;
  856. if (daysToDayOfWeek > end) {
  857. daysToDayOfWeek -= 7;
  858. }
  859. if (daysToDayOfWeek < end - 7) {
  860. daysToDayOfWeek += 7;
  861. }
  862. adjustedMoment = moment(mom).add('d', daysToDayOfWeek);
  863. return {
  864. week: Math.ceil(adjustedMoment.dayOfYear() / 7),
  865. year: adjustedMoment.year()
  866. };
  867. }
  868. /************************************
  869. Top Level Functions
  870. ************************************/
  871. function makeMoment(config) {
  872. var input = config._i,
  873. format = config._f;
  874. if (input === null || input === '') {
  875. return null;
  876. }
  877. if (typeof input === 'string') {
  878. config._i = input = getLangDefinition().preparse(input);
  879. }
  880. if (moment.isMoment(input)) {
  881. config = extend({}, input);
  882. config._d = new Date(+input._d);
  883. } else if (format) {
  884. if (isArray(format)) {
  885. makeDateFromStringAndArray(config);
  886. } else {
  887. makeDateFromStringAndFormat(config);
  888. }
  889. } else {
  890. makeDateFromInput(config);
  891. }
  892. return new Moment(config);
  893. }
  894. moment = function (input, format, lang) {
  895. return makeMoment({
  896. _i : input,
  897. _f : format,
  898. _l : lang,
  899. _isUTC : false
  900. });
  901. };
  902. // creating with utc
  903. moment.utc = function (input, format, lang) {
  904. return makeMoment({
  905. _useUTC : true,
  906. _isUTC : true,
  907. _l : lang,
  908. _i : input,
  909. _f : format
  910. });
  911. };
  912. // creating with unix timestamp (in seconds)
  913. moment.unix = function (input) {
  914. return moment(input * 1000);
  915. };
  916. // duration
  917. moment.duration = function (input, key) {
  918. var isDuration = moment.isDuration(input),
  919. isNumber = (typeof input === 'number'),
  920. duration = (isDuration ? input._input : (isNumber ? {} : input)),
  921. matched = aspNetTimeSpanJsonRegex.exec(input),
  922. sign,
  923. ret;
  924. if (isNumber) {
  925. if (key) {
  926. duration[key] = input;
  927. } else {
  928. duration.milliseconds = input;
  929. }
  930. } else if (matched) {
  931. sign = (matched[1] === "-") ? -1 : 1;
  932. duration = {
  933. y: 0,
  934. d: ~~matched[2] * sign,
  935. h: ~~matched[3] * sign,
  936. m: ~~matched[4] * sign,
  937. s: ~~matched[5] * sign,
  938. ms: ~~matched[6] * sign
  939. };
  940. }
  941. ret = new Duration(duration);
  942. if (isDuration && input.hasOwnProperty('_lang')) {
  943. ret._lang = input._lang;
  944. }
  945. return ret;
  946. };
  947. // version number
  948. moment.version = VERSION;
  949. // default format
  950. moment.defaultFormat = isoFormat;
  951. // This function will be called whenever a moment is mutated.
  952. // It is intended to keep the offset in sync with the timezone.
  953. moment.updateOffset = function () {};
  954. // This function will load languages and then set the global language. If
  955. // no arguments are passed in, it will simply return the current global
  956. // language key.
  957. moment.lang = function (key, values) {
  958. if (!key) {
  959. return moment.fn._lang._abbr;
  960. }
  961. if (values) {
  962. loadLang(key, values);
  963. } else if (!languages[key]) {
  964. getLangDefinition(key);
  965. }
  966. moment.duration.fn._lang = moment.fn._lang = getLangDefinition(key);
  967. };
  968. // returns language data
  969. moment.langData = function (key) {
  970. if (key && key._lang && key._lang._abbr) {
  971. key = key._lang._abbr;
  972. }
  973. return getLangDefinition(key);
  974. };
  975. // compare moment object
  976. moment.isMoment = function (obj) {
  977. return obj instanceof Moment;
  978. };
  979. // for typechecking Duration objects
  980. moment.isDuration = function (obj) {
  981. return obj instanceof Duration;
  982. };
  983. /************************************
  984. Moment Prototype
  985. ************************************/
  986. moment.fn = Moment.prototype = {
  987. clone : function () {
  988. return moment(this);
  989. },
  990. valueOf : function () {
  991. return +this._d + ((this._offset || 0) * 60000);
  992. },
  993. unix : function () {
  994. return Math.floor(+this / 1000);
  995. },
  996. toString : function () {
  997. return this.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ");
  998. },
  999. toDate : function () {
  1000. return this._offset ? new Date(+this) : this._d;
  1001. },
  1002. toISOString : function () {
  1003. return formatMoment(moment(this).utc(), 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
  1004. },
  1005. toArray : function () {
  1006. var m = this;
  1007. return [
  1008. m.year(),
  1009. m.month(),
  1010. m.date(),
  1011. m.hours(),
  1012. m.minutes(),
  1013. m.seconds(),
  1014. m.milliseconds()
  1015. ];
  1016. },
  1017. isValid : function () {
  1018. if (this._isValid == null) {
  1019. if (this._a) {
  1020. this._isValid = !compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray());
  1021. } else {
  1022. this._isValid = !isNaN(this._d.getTime());
  1023. }
  1024. }
  1025. return !!this._isValid;
  1026. },
  1027. utc : function () {
  1028. return this.zone(0);
  1029. },
  1030. local : function () {
  1031. this.zone(0);
  1032. this._isUTC = false;
  1033. return this;
  1034. },
  1035. format : function (inputString) {
  1036. var output = formatMoment(this, inputString || moment.defaultFormat);
  1037. return this.lang().postformat(output);
  1038. },
  1039. add : function (input, val) {
  1040. var dur;
  1041. // switch args to support add('s', 1) and add(1, 's')
  1042. if (typeof input === 'string') {
  1043. dur = moment.duration(+val, input);
  1044. } else {
  1045. dur = moment.duration(input, val);
  1046. }
  1047. addOrSubtractDurationFromMoment(this, dur, 1);
  1048. return this;
  1049. },
  1050. subtract : function (input, val) {
  1051. var dur;
  1052. // switch args to support subtract('s', 1) and subtract(1, 's')
  1053. if (typeof input === 'string') {
  1054. dur = moment.duration(+val, input);
  1055. } else {
  1056. dur = moment.duration(input, val);
  1057. }
  1058. addOrSubtractDurationFromMoment(this, dur, -1);
  1059. return this;
  1060. },
  1061. diff : function (input, units, asFloat) {
  1062. var that = this._isUTC ? moment(input).zone(this._offset || 0) : moment(input).local(),
  1063. zoneDiff = (this.zone() - that.zone()) * 6e4,
  1064. diff, output;
  1065. units = normalizeUnits(units);
  1066. if (units === 'year' || units === 'month') {
  1067. // average number of days in the months in the given dates
  1068. diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
  1069. // difference in months
  1070. output = ((this.year() - that.year()) * 12) + (this.month() - that.month());
  1071. // adjust by taking difference in days, average number of days
  1072. // and dst in the given months.
  1073. output += ((this - moment(this).startOf('month')) -
  1074. (that - moment(that).startOf('month'))) / diff;
  1075. // same as above but with zones, to negate all dst
  1076. output -= ((this.zone() - moment(this).startOf('month').zone()) -
  1077. (that.zone() - moment(that).startOf('month').zone())) * 6e4 / diff;
  1078. if (units === 'year') {
  1079. output = output / 12;
  1080. }
  1081. } else {
  1082. diff = (this - that);
  1083. output = units === 'second' ? diff / 1e3 : // 1000
  1084. units === 'minute' ? diff / 6e4 : // 1000 * 60
  1085. units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
  1086. units === 'day' ? (diff - zoneDiff) / 864e5 : // 1000 * 60 * 60 * 24, negate dst
  1087. units === 'week' ? (diff - zoneDiff) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst
  1088. diff;
  1089. }
  1090. return asFloat ? output : absRound(output);
  1091. },
  1092. from : function (time, withoutSuffix) {
  1093. return moment.duration(this.diff(time)).lang(this.lang()._abbr).humanize(!withoutSuffix);
  1094. },
  1095. fromNow : function (withoutSuffix) {
  1096. return this.from(moment(), withoutSuffix);
  1097. },
  1098. calendar : function () {
  1099. var diff = this.diff(moment().startOf('day'), 'days', true),
  1100. format = diff < -6 ? 'sameElse' :
  1101. diff < -1 ? 'lastWeek' :
  1102. diff < 0 ? 'lastDay' :
  1103. diff < 1 ? 'sameDay' :
  1104. diff < 2 ? 'nextDay' :
  1105. diff < 7 ? 'nextWeek' : 'sameElse';
  1106. return this.format(this.lang().calendar(format, this));
  1107. },
  1108. isLeapYear : function () {
  1109. var year = this.year();
  1110. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  1111. },
  1112. isDST : function () {
  1113. return (this.zone() < this.clone().month(0).zone() ||
  1114. this.zone() < this.clone().month(5).zone());
  1115. },
  1116. day : function (input) {
  1117. var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
  1118. if (input != null) {
  1119. if (typeof input === 'string') {
  1120. input = this.lang().weekdaysParse(input);
  1121. if (typeof input !== 'number') {
  1122. return this;
  1123. }
  1124. }
  1125. return this.add({ d : input - day });
  1126. } else {
  1127. return day;
  1128. }
  1129. },
  1130. month : function (input) {
  1131. var utc = this._isUTC ? 'UTC' : '',
  1132. dayOfMonth,
  1133. daysInMonth;
  1134. if (input != null) {
  1135. if (typeof input === 'string') {
  1136. input = this.lang().monthsParse(input);
  1137. if (typeof input !== 'number') {
  1138. return this;
  1139. }
  1140. }
  1141. dayOfMonth = this.date();
  1142. this.date(1);
  1143. this._d['set' + utc + 'Month'](input);
  1144. this.date(Math.min(dayOfMonth, this.daysInMonth()));
  1145. moment.updateOffset(this);
  1146. return this;
  1147. } else {
  1148. return this._d['get' + utc + 'Month']();
  1149. }
  1150. },
  1151. startOf: function (units) {
  1152. units = normalizeUnits(units);
  1153. // the following switch intentionally omits break keywords
  1154. // to utilize falling through the cases.
  1155. switch (units) {
  1156. case 'year':
  1157. this.month(0);
  1158. /* falls through */
  1159. case 'month':
  1160. this.date(1);
  1161. /* falls through */
  1162. case 'week':
  1163. case 'day':
  1164. this.hours(0);
  1165. /* falls through */
  1166. case 'hour':
  1167. this.minutes(0);
  1168. /* falls through */
  1169. case 'minute':
  1170. this.seconds(0);
  1171. /* falls through */
  1172. case 'second':
  1173. this.milliseconds(0);
  1174. /* falls through */
  1175. }
  1176. // weeks are a special case
  1177. if (units === 'week') {
  1178. this.weekday(0);
  1179. }
  1180. return this;
  1181. },
  1182. endOf: function (units) {
  1183. return this.startOf(units).add(units, 1).subtract('ms', 1);
  1184. },
  1185. isAfter: function (input, units) {
  1186. units = typeof units !== 'undefined' ? units : 'millisecond';
  1187. return +this.clone().startOf(units) > +moment(input).startOf(units);
  1188. },
  1189. isBefore: function (input, units) {
  1190. units = typeof units !== 'undefined' ? units : 'millisecond';
  1191. return +this.clone().startOf(units) < +moment(input).startOf(units);
  1192. },
  1193. isSame: function (input, units) {
  1194. units = typeof units !== 'undefined' ? units : 'millisecond';
  1195. return +this.clone().startOf(units) === +moment(input).startOf(units);
  1196. },
  1197. min: function (other) {
  1198. other = moment.apply(null, arguments);
  1199. return other < this ? this : other;
  1200. },
  1201. max: function (other) {
  1202. other = moment.apply(null, arguments);
  1203. return other > this ? this : other;
  1204. },
  1205. zone : function (input) {
  1206. var offset = this._offset || 0;
  1207. if (input != null) {
  1208. if (typeof input === "string") {
  1209. input = timezoneMinutesFromString(input);
  1210. }
  1211. if (Math.abs(input) < 16) {
  1212. input = input * 60;
  1213. }
  1214. this._offset = input;
  1215. this._isUTC = true;
  1216. if (offset !== input) {
  1217. addOrSubtractDurationFromMoment(this, moment.duration(offset - input, 'm'), 1, true);
  1218. }
  1219. } else {
  1220. return this._isUTC ? offset : this._d.getTimezoneOffset();
  1221. }
  1222. return this;
  1223. },
  1224. zoneAbbr : function () {
  1225. return this._isUTC ? "UTC" : "";
  1226. },
  1227. zoneName : function () {
  1228. return this._isUTC ? "Coordinated Universal Time" : "";
  1229. },
  1230. daysInMonth : function () {
  1231. return moment.utc([this.year(), this.month() + 1, 0]).date();
  1232. },
  1233. dayOfYear : function (input) {
  1234. var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
  1235. return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
  1236. },
  1237. weekYear : function (input) {
  1238. var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year;
  1239. return input == null ? year : this.add("y", (input - year));
  1240. },
  1241. isoWeekYear : function (input) {
  1242. var year = weekOfYear(this, 1, 4).year;
  1243. return input == null ? year : this.add("y", (input - year));
  1244. },
  1245. week : function (input) {
  1246. var week = this.lang().week(this);
  1247. return input == null ? week : this.add("d", (input - week) * 7);
  1248. },
  1249. isoWeek : function (input) {
  1250. var week = weekOfYear(this, 1, 4).week;
  1251. return input == null ? week : this.add("d", (input - week) * 7);
  1252. },
  1253. weekday : function (input) {
  1254. var weekday = (this._d.getDay() + 7 - this.lang()._week.dow) % 7;
  1255. return input == null ? weekday : this.add("d", input - weekday);
  1256. },
  1257. isoWeekday : function (input) {
  1258. // behaves the same as moment#day except
  1259. // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
  1260. // as a setter, sunday should belong to the previous week.
  1261. return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);
  1262. },
  1263. // If passed a language key, it will set the language for this
  1264. // instance. Otherwise, it will return the language configuration
  1265. // variables for this instance.
  1266. lang : function (key) {
  1267. if (key === undefined) {
  1268. return this._lang;
  1269. } else {
  1270. this._lang = getLangDefinition(key);
  1271. return this;
  1272. }
  1273. }
  1274. };
  1275. // helper for adding shortcuts
  1276. function makeGetterAndSetter(name, key) {
  1277. moment.fn[name] = moment.fn[name + 's'] = function (input) {
  1278. var utc = this._isUTC ? 'UTC' : '';
  1279. if (input != null) {
  1280. this._d['set' + utc + key](input);
  1281. moment.updateOffset(this);
  1282. return this;
  1283. } else {
  1284. return this._d['get' + utc + key]();
  1285. }
  1286. };
  1287. }
  1288. // loop through and add shortcuts (Month, Date, Hours, Minutes, Seconds, Milliseconds)
  1289. for (i = 0; i < proxyGettersAndSetters.length; i ++) {
  1290. makeGetterAndSetter(proxyGettersAndSetters[i].toLowerCase().replace(/s$/, ''), proxyGettersAndSetters[i]);
  1291. }
  1292. // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
  1293. makeGetterAndSetter('year', 'FullYear');
  1294. // add plural methods
  1295. moment.fn.days = moment.fn.day;
  1296. moment.fn.months = moment.fn.month;
  1297. moment.fn.weeks = moment.fn.week;
  1298. moment.fn.isoWeeks = moment.fn.isoWeek;
  1299. // add aliased format methods
  1300. moment.fn.toJSON = moment.fn.toISOString;
  1301. /************************************
  1302. Duration Prototype
  1303. ************************************/
  1304. moment.duration.fn = Duration.prototype = {
  1305. _bubble : function () {
  1306. var milliseconds = this._milliseconds,
  1307. days = this._days,
  1308. months = this._months,
  1309. data = this._data,
  1310. seconds, minutes, hours, years;
  1311. // The following code bubbles up values, see the tests for
  1312. // examples of what that means.
  1313. data.milliseconds = milliseconds % 1000;
  1314. seconds = absRound(milliseconds / 1000);
  1315. data.seconds = seconds % 60;
  1316. minutes = absRound(seconds / 60);
  1317. data.minutes = minutes % 60;
  1318. hours = absRound(minutes / 60);
  1319. data.hours = hours % 24;
  1320. days += absRound(hours / 24);
  1321. data.days = days % 30;
  1322. months += absRound(days / 30);
  1323. data.months = months % 12;
  1324. years = absRound(months / 12);
  1325. data.years = years;
  1326. },
  1327. weeks : function () {
  1328. return absRound(this.days() / 7);
  1329. },
  1330. valueOf : function () {
  1331. return this._milliseconds +
  1332. this._days * 864e5 +
  1333. (this._months % 12) * 2592e6 +
  1334. ~~(this._months / 12) * 31536e6;
  1335. },
  1336. humanize : function (withSuffix) {
  1337. var difference = +this,
  1338. output = relativeTime(difference, !withSuffix, this.lang());
  1339. if (withSuffix) {
  1340. output = this.lang().pastFuture(difference, output);
  1341. }
  1342. return this.lang().postformat(output);
  1343. },
  1344. add : function (input, val) {
  1345. // supports only 2.0-style add(1, 's') or add(moment)
  1346. var dur = moment.duration(input, val);
  1347. this._milliseconds += dur._milliseconds;
  1348. this._days += dur._days;
  1349. this._months += dur._months;
  1350. this._bubble();
  1351. return this;
  1352. },
  1353. subtract : function (input, val) {
  1354. var dur = moment.duration(input, val);
  1355. this._milliseconds -= dur._milliseconds;
  1356. this._days -= dur._days;
  1357. this._months -= dur._months;
  1358. this._bubble();
  1359. return this;
  1360. },
  1361. get : function (units) {
  1362. units = normalizeUnits(units);
  1363. return this[units.toLowerCase() + 's']();
  1364. },
  1365. as : function (units) {
  1366. units = normalizeUnits(units);
  1367. return this['as' + units.charAt(0).toUpperCase() + units.slice(1) + 's']();
  1368. },
  1369. lang : moment.fn.lang
  1370. };
  1371. function makeDurationGetter(name) {
  1372. moment.duration.fn[name] = function () {
  1373. return this._data[name];
  1374. };
  1375. }
  1376. function makeDurationAsGetter(name, factor) {
  1377. moment.duration.fn['as' + name] = function () {
  1378. return +this / factor;
  1379. };
  1380. }
  1381. for (i in unitMillisecondFactors) {
  1382. if (unitMillisecondFactors.hasOwnProperty(i)) {
  1383. makeDurationAsGetter(i, unitMillisecondFactors[i]);
  1384. makeDurationGetter(i.toLowerCase());
  1385. }
  1386. }
  1387. makeDurationAsGetter('Weeks', 6048e5);
  1388. moment.duration.fn.asMonths = function () {
  1389. return (+this - this.years() * 31536e6) / 2592e6 + this.years() * 12;
  1390. };
  1391. /************************************
  1392. Default Lang
  1393. ************************************/
  1394. // Set default language, other languages will inherit from English.
  1395. moment.lang('en', {
  1396. ordinal : function (number) {
  1397. var b = number % 10,
  1398. output = (~~ (number % 100 / 10) === 1) ? 'th' :
  1399. (b === 1) ? 'st' :
  1400. (b === 2) ? 'nd' :
  1401. (b === 3) ? 'rd' : 'th';
  1402. return number + output;
  1403. }
  1404. });
  1405. /************************************
  1406. Exposing Moment
  1407. ************************************/
  1408. // CommonJS module is defined
  1409. if (hasModule) {
  1410. module.exports = moment;
  1411. }
  1412. /*global ender:false */
  1413. if (typeof ender === 'undefined') {
  1414. // here, `this` means `window` in the browser, or `global` on the server
  1415. // add `moment` as a global object via a string identifier,
  1416. // for Closure Compiler "advanced" mode
  1417. this['moment'] = moment;
  1418. }
  1419. /*global define:false */
  1420. if (typeof define === "function" && define.amd) {
  1421. define("moment", [], function () {
  1422. return moment;
  1423. });
  1424. }
  1425. }).call(this);