You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2804 lines
89 KiB

8 years ago
  1. /* Copyright 2017 Tim Wood, Iskren Chernev, Moment.js contributors
  2. * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
  3. (function (undefined) {
  4. /************************************
  5. Constants
  6. ************************************/
  7. var moment,
  8. VERSION = '2.8.1',
  9. // the global-scope this is NOT the global object in Node.js
  10. globalScope = typeof global !== 'undefined' ? global : this,
  11. oldGlobalMoment,
  12. round = Math.round,
  13. i,
  14. YEAR = 0,
  15. MONTH = 1,
  16. DATE = 2,
  17. HOUR = 3,
  18. MINUTE = 4,
  19. SECOND = 5,
  20. MILLISECOND = 6,
  21. // internal storage for locale config files
  22. locales = {},
  23. // extra moment internal properties (plugins register props here)
  24. momentProperties = [],
  25. // check for nodeJS
  26. hasModule = (typeof module !== 'undefined' && module.exports),
  27. // ASP.NET json date format regex
  28. aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
  29. aspNetTimeSpanJsonRegex = /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/,
  30. // from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html
  31. // somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere
  32. isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,
  33. // format tokens
  34. formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g,
  35. localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
  36. // parsing token regexes
  37. parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99
  38. parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999
  39. parseTokenOneToFourDigits = /\d{1,4}/, // 0 - 9999
  40. parseTokenOneToSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999
  41. parseTokenDigits = /\d+/, // nonzero number of digits
  42. 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.
  43. parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/gi, // +00:00 -00:00 +0000 -0000 or Z
  44. parseTokenT = /T/i, // T (ISO separator)
  45. parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
  46. parseTokenOrdinal = /\d{1,2}/,
  47. //strict parsing regexes
  48. parseTokenOneDigit = /\d/, // 0 - 9
  49. parseTokenTwoDigits = /\d\d/, // 00 - 99
  50. parseTokenThreeDigits = /\d{3}/, // 000 - 999
  51. parseTokenFourDigits = /\d{4}/, // 0000 - 9999
  52. parseTokenSixDigits = /[+-]?\d{6}/, // -999,999 - 999,999
  53. parseTokenSignedNumber = /[+-]?\d+/, // -inf - inf
  54. // iso 8601 regex
  55. // 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00)
  56. isoRegex = /^\s*(?:[+-]\d{6}|\d{4})-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?$/,
  57. isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
  58. isoDates = [
  59. ['YYYYYY-MM-DD', /[+-]\d{6}-\d{2}-\d{2}/],
  60. ['YYYY-MM-DD', /\d{4}-\d{2}-\d{2}/],
  61. ['GGGG-[W]WW-E', /\d{4}-W\d{2}-\d/],
  62. ['GGGG-[W]WW', /\d{4}-W\d{2}/],
  63. ['YYYY-DDD', /\d{4}-\d{3}/]
  64. ],
  65. // iso time formats and regexes
  66. isoTimes = [
  67. ['HH:mm:ss.SSSS', /(T| )\d\d:\d\d:\d\d\.\d+/],
  68. ['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/],
  69. ['HH:mm', /(T| )\d\d:\d\d/],
  70. ['HH', /(T| )\d\d/]
  71. ],
  72. // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
  73. parseTimezoneChunker = /([\+\-]|\d\d)/gi,
  74. // getter and setter names
  75. proxyGettersAndSetters = 'Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
  76. unitMillisecondFactors = {
  77. 'Milliseconds' : 1,
  78. 'Seconds' : 1e3,
  79. 'Minutes' : 6e4,
  80. 'Hours' : 36e5,
  81. 'Days' : 864e5,
  82. 'Months' : 2592e6,
  83. 'Years' : 31536e6
  84. },
  85. unitAliases = {
  86. ms : 'millisecond',
  87. s : 'second',
  88. m : 'minute',
  89. h : 'hour',
  90. d : 'day',
  91. D : 'date',
  92. w : 'week',
  93. W : 'isoWeek',
  94. M : 'month',
  95. Q : 'quarter',
  96. y : 'year',
  97. DDD : 'dayOfYear',
  98. e : 'weekday',
  99. E : 'isoWeekday',
  100. gg: 'weekYear',
  101. GG: 'isoWeekYear'
  102. },
  103. camelFunctions = {
  104. dayofyear : 'dayOfYear',
  105. isoweekday : 'isoWeekday',
  106. isoweek : 'isoWeek',
  107. weekyear : 'weekYear',
  108. isoweekyear : 'isoWeekYear'
  109. },
  110. // format function strings
  111. formatFunctions = {},
  112. // default relative time thresholds
  113. relativeTimeThresholds = {
  114. s: 45, // seconds to minute
  115. m: 45, // minutes to hour
  116. h: 22, // hours to day
  117. d: 26, // days to month
  118. M: 11 // months to year
  119. },
  120. // tokens to ordinalize and pad
  121. ordinalizeTokens = 'DDD w W M D d'.split(' '),
  122. paddedTokens = 'M D H h m s w W'.split(' '),
  123. formatTokenFunctions = {
  124. M : function () {
  125. return this.month() + 1;
  126. },
  127. MMM : function (format) {
  128. return this.localeData().monthsShort(this, format);
  129. },
  130. MMMM : function (format) {
  131. return this.localeData().months(this, format);
  132. },
  133. D : function () {
  134. return this.date();
  135. },
  136. DDD : function () {
  137. return this.dayOfYear();
  138. },
  139. d : function () {
  140. return this.day();
  141. },
  142. dd : function (format) {
  143. return this.localeData().weekdaysMin(this, format);
  144. },
  145. ddd : function (format) {
  146. return this.localeData().weekdaysShort(this, format);
  147. },
  148. dddd : function (format) {
  149. return this.localeData().weekdays(this, format);
  150. },
  151. w : function () {
  152. return this.week();
  153. },
  154. W : function () {
  155. return this.isoWeek();
  156. },
  157. YY : function () {
  158. return leftZeroFill(this.year() % 100, 2);
  159. },
  160. YYYY : function () {
  161. return leftZeroFill(this.year(), 4);
  162. },
  163. YYYYY : function () {
  164. return leftZeroFill(this.year(), 5);
  165. },
  166. YYYYYY : function () {
  167. var y = this.year(), sign = y >= 0 ? '+' : '-';
  168. return sign + leftZeroFill(Math.abs(y), 6);
  169. },
  170. gg : function () {
  171. return leftZeroFill(this.weekYear() % 100, 2);
  172. },
  173. gggg : function () {
  174. return leftZeroFill(this.weekYear(), 4);
  175. },
  176. ggggg : function () {
  177. return leftZeroFill(this.weekYear(), 5);
  178. },
  179. GG : function () {
  180. return leftZeroFill(this.isoWeekYear() % 100, 2);
  181. },
  182. GGGG : function () {
  183. return leftZeroFill(this.isoWeekYear(), 4);
  184. },
  185. GGGGG : function () {
  186. return leftZeroFill(this.isoWeekYear(), 5);
  187. },
  188. e : function () {
  189. return this.weekday();
  190. },
  191. E : function () {
  192. return this.isoWeekday();
  193. },
  194. a : function () {
  195. return this.localeData().meridiem(this.hours(), this.minutes(), true);
  196. },
  197. A : function () {
  198. return this.localeData().meridiem(this.hours(), this.minutes(), false);
  199. },
  200. H : function () {
  201. return this.hours();
  202. },
  203. h : function () {
  204. return this.hours() % 12 || 12;
  205. },
  206. m : function () {
  207. return this.minutes();
  208. },
  209. s : function () {
  210. return this.seconds();
  211. },
  212. S : function () {
  213. return toInt(this.milliseconds() / 100);
  214. },
  215. SS : function () {
  216. return leftZeroFill(toInt(this.milliseconds() / 10), 2);
  217. },
  218. SSS : function () {
  219. return leftZeroFill(this.milliseconds(), 3);
  220. },
  221. SSSS : function () {
  222. return leftZeroFill(this.milliseconds(), 3);
  223. },
  224. Z : function () {
  225. var a = -this.zone(),
  226. b = '+';
  227. if (a < 0) {
  228. a = -a;
  229. b = '-';
  230. }
  231. return b + leftZeroFill(toInt(a / 60), 2) + ':' + leftZeroFill(toInt(a) % 60, 2);
  232. },
  233. ZZ : function () {
  234. var a = -this.zone(),
  235. b = '+';
  236. if (a < 0) {
  237. a = -a;
  238. b = '-';
  239. }
  240. return b + leftZeroFill(toInt(a / 60), 2) + leftZeroFill(toInt(a) % 60, 2);
  241. },
  242. z : function () {
  243. return this.zoneAbbr();
  244. },
  245. zz : function () {
  246. return this.zoneName();
  247. },
  248. X : function () {
  249. return this.unix();
  250. },
  251. Q : function () {
  252. return this.quarter();
  253. }
  254. },
  255. deprecations = {},
  256. lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin'];
  257. // Pick the first defined of two or three arguments. dfl comes from
  258. // default.
  259. function dfl(a, b, c) {
  260. switch (arguments.length) {
  261. case 2: return a != null ? a : b;
  262. case 3: return a != null ? a : b != null ? b : c;
  263. default: throw new Error('Implement me');
  264. }
  265. }
  266. function defaultParsingFlags() {
  267. // We need to deep clone this object, and es5 standard is not very
  268. // helpful.
  269. return {
  270. empty : false,
  271. unusedTokens : [],
  272. unusedInput : [],
  273. overflow : -2,
  274. charsLeftOver : 0,
  275. nullInput : false,
  276. invalidMonth : null,
  277. invalidFormat : false,
  278. userInvalidated : false,
  279. iso: false
  280. };
  281. }
  282. function printMsg(msg) {
  283. if (moment.suppressDeprecationWarnings === false &&
  284. typeof console !== 'undefined' && console.warn) {
  285. console.warn("Deprecation warning: " + msg);
  286. }
  287. }
  288. function deprecate(msg, fn) {
  289. var firstTime = true;
  290. return extend(function () {
  291. if (firstTime) {
  292. printMsg(msg);
  293. firstTime = false;
  294. }
  295. return fn.apply(this, arguments);
  296. }, fn);
  297. }
  298. function deprecateSimple(name, msg) {
  299. if (!deprecations[name]) {
  300. printMsg(msg);
  301. deprecations[name] = true;
  302. }
  303. }
  304. function padToken(func, count) {
  305. return function (a) {
  306. return leftZeroFill(func.call(this, a), count);
  307. };
  308. }
  309. function ordinalizeToken(func, period) {
  310. return function (a) {
  311. return this.localeData().ordinal(func.call(this, a), period);
  312. };
  313. }
  314. while (ordinalizeTokens.length) {
  315. i = ordinalizeTokens.pop();
  316. formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i], i);
  317. }
  318. while (paddedTokens.length) {
  319. i = paddedTokens.pop();
  320. formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2);
  321. }
  322. formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
  323. /************************************
  324. Constructors
  325. ************************************/
  326. function Locale() {
  327. }
  328. // Moment prototype object
  329. function Moment(config, skipOverflow) {
  330. if (skipOverflow !== false) {
  331. checkOverflow(config);
  332. }
  333. copyConfig(this, config);
  334. this._d = new Date(+config._d);
  335. }
  336. // Duration Constructor
  337. function Duration(duration) {
  338. var normalizedInput = normalizeObjectUnits(duration),
  339. years = normalizedInput.year || 0,
  340. quarters = normalizedInput.quarter || 0,
  341. months = normalizedInput.month || 0,
  342. weeks = normalizedInput.week || 0,
  343. days = normalizedInput.day || 0,
  344. hours = normalizedInput.hour || 0,
  345. minutes = normalizedInput.minute || 0,
  346. seconds = normalizedInput.second || 0,
  347. milliseconds = normalizedInput.millisecond || 0;
  348. // representation for dateAddRemove
  349. this._milliseconds = +milliseconds +
  350. seconds * 1e3 + // 1000
  351. minutes * 6e4 + // 1000 * 60
  352. hours * 36e5; // 1000 * 60 * 60
  353. // Because of dateAddRemove treats 24 hours as different from a
  354. // day when working around DST, we need to store them separately
  355. this._days = +days +
  356. weeks * 7;
  357. // It is impossible translate months into days without knowing
  358. // which months you are are talking about, so we have to store
  359. // it separately.
  360. this._months = +months +
  361. quarters * 3 +
  362. years * 12;
  363. this._data = {};
  364. this._locale = moment.localeData();
  365. this._bubble();
  366. }
  367. /************************************
  368. Helpers
  369. ************************************/
  370. function extend(a, b) {
  371. for (var i in b) {
  372. if (b.hasOwnProperty(i)) {
  373. a[i] = b[i];
  374. }
  375. }
  376. if (b.hasOwnProperty('toString')) {
  377. a.toString = b.toString;
  378. }
  379. if (b.hasOwnProperty('valueOf')) {
  380. a.valueOf = b.valueOf;
  381. }
  382. return a;
  383. }
  384. function copyConfig(to, from) {
  385. var i, prop, val;
  386. if (typeof from._isAMomentObject !== 'undefined') {
  387. to._isAMomentObject = from._isAMomentObject;
  388. }
  389. if (typeof from._i !== 'undefined') {
  390. to._i = from._i;
  391. }
  392. if (typeof from._f !== 'undefined') {
  393. to._f = from._f;
  394. }
  395. if (typeof from._l !== 'undefined') {
  396. to._l = from._l;
  397. }
  398. if (typeof from._strict !== 'undefined') {
  399. to._strict = from._strict;
  400. }
  401. if (typeof from._tzm !== 'undefined') {
  402. to._tzm = from._tzm;
  403. }
  404. if (typeof from._isUTC !== 'undefined') {
  405. to._isUTC = from._isUTC;
  406. }
  407. if (typeof from._offset !== 'undefined') {
  408. to._offset = from._offset;
  409. }
  410. if (typeof from._pf !== 'undefined') {
  411. to._pf = from._pf;
  412. }
  413. if (typeof from._locale !== 'undefined') {
  414. to._locale = from._locale;
  415. }
  416. if (momentProperties.length > 0) {
  417. for (i in momentProperties) {
  418. prop = momentProperties[i];
  419. val = from[prop];
  420. if (typeof val !== 'undefined') {
  421. to[prop] = val;
  422. }
  423. }
  424. }
  425. return to;
  426. }
  427. function absRound(number) {
  428. if (number < 0) {
  429. return Math.ceil(number);
  430. } else {
  431. return Math.floor(number);
  432. }
  433. }
  434. // left zero fill a number
  435. // see http://jsperf.com/left-zero-filling for performance comparison
  436. function leftZeroFill(number, targetLength, forceSign) {
  437. var output = '' + Math.abs(number),
  438. sign = number >= 0;
  439. while (output.length < targetLength) {
  440. output = '0' + output;
  441. }
  442. return (sign ? (forceSign ? '+' : '') : '-') + output;
  443. }
  444. function positiveMomentsDifference(base, other) {
  445. var res = {milliseconds: 0, months: 0};
  446. res.months = other.month() - base.month() +
  447. (other.year() - base.year()) * 12;
  448. if (base.clone().add(res.months, 'M').isAfter(other)) {
  449. --res.months;
  450. }
  451. res.milliseconds = +other - +(base.clone().add(res.months, 'M'));
  452. return res;
  453. }
  454. function momentsDifference(base, other) {
  455. var res;
  456. other = makeAs(other, base);
  457. if (base.isBefore(other)) {
  458. res = positiveMomentsDifference(base, other);
  459. } else {
  460. res = positiveMomentsDifference(other, base);
  461. res.milliseconds = -res.milliseconds;
  462. res.months = -res.months;
  463. }
  464. return res;
  465. }
  466. // TODO: remove 'name' arg after deprecation is removed
  467. function createAdder(direction, name) {
  468. return function (val, period) {
  469. var dur, tmp;
  470. //invert the arguments, but complain about it
  471. if (period !== null && !isNaN(+period)) {
  472. deprecateSimple(name, "moment()." + name + "(period, number) is deprecated. Please use moment()." + name + "(number, period).");
  473. tmp = val; val = period; period = tmp;
  474. }
  475. val = typeof val === 'string' ? +val : val;
  476. dur = moment.duration(val, period);
  477. addOrSubtractDurationFromMoment(this, dur, direction);
  478. return this;
  479. };
  480. }
  481. function addOrSubtractDurationFromMoment(mom, duration, isAdding, updateOffset) {
  482. var milliseconds = duration._milliseconds,
  483. days = duration._days,
  484. months = duration._months;
  485. updateOffset = updateOffset == null ? true : updateOffset;
  486. if (milliseconds) {
  487. mom._d.setTime(+mom._d + milliseconds * isAdding);
  488. }
  489. if (days) {
  490. rawSetter(mom, 'Date', rawGetter(mom, 'Date') + days * isAdding);
  491. }
  492. if (months) {
  493. rawMonthSetter(mom, rawGetter(mom, 'Month') + months * isAdding);
  494. }
  495. if (updateOffset) {
  496. moment.updateOffset(mom, days || months);
  497. }
  498. }
  499. // check if is an array
  500. function isArray(input) {
  501. return Object.prototype.toString.call(input) === '[object Array]';
  502. }
  503. function isDate(input) {
  504. return Object.prototype.toString.call(input) === '[object Date]' ||
  505. input instanceof Date;
  506. }
  507. // compare two arrays, return the number of differences
  508. function compareArrays(array1, array2, dontConvert) {
  509. var len = Math.min(array1.length, array2.length),
  510. lengthDiff = Math.abs(array1.length - array2.length),
  511. diffs = 0,
  512. i;
  513. for (i = 0; i < len; i++) {
  514. if ((dontConvert && array1[i] !== array2[i]) ||
  515. (!dontConvert && toInt(array1[i]) !== toInt(array2[i]))) {
  516. diffs++;
  517. }
  518. }
  519. return diffs + lengthDiff;
  520. }
  521. function normalizeUnits(units) {
  522. if (units) {
  523. var lowered = units.toLowerCase().replace(/(.)s$/, '$1');
  524. units = unitAliases[units] || camelFunctions[lowered] || lowered;
  525. }
  526. return units;
  527. }
  528. function normalizeObjectUnits(inputObject) {
  529. var normalizedInput = {},
  530. normalizedProp,
  531. prop;
  532. for (prop in inputObject) {
  533. if (inputObject.hasOwnProperty(prop)) {
  534. normalizedProp = normalizeUnits(prop);
  535. if (normalizedProp) {
  536. normalizedInput[normalizedProp] = inputObject[prop];
  537. }
  538. }
  539. }
  540. return normalizedInput;
  541. }
  542. function makeList(field) {
  543. var count, setter;
  544. if (field.indexOf('week') === 0) {
  545. count = 7;
  546. setter = 'day';
  547. }
  548. else if (field.indexOf('month') === 0) {
  549. count = 12;
  550. setter = 'month';
  551. }
  552. else {
  553. return;
  554. }
  555. moment[field] = function (format, index) {
  556. var i, getter,
  557. method = moment._locale[field],
  558. results = [];
  559. if (typeof format === 'number') {
  560. index = format;
  561. format = undefined;
  562. }
  563. getter = function (i) {
  564. var m = moment().utc().set(setter, i);
  565. return method.call(moment._locale, m, format || '');
  566. };
  567. if (index != null) {
  568. return getter(index);
  569. }
  570. else {
  571. for (i = 0; i < count; i++) {
  572. results.push(getter(i));
  573. }
  574. return results;
  575. }
  576. };
  577. }
  578. function toInt(argumentForCoercion) {
  579. var coercedNumber = +argumentForCoercion,
  580. value = 0;
  581. if (coercedNumber !== 0 && isFinite(coercedNumber)) {
  582. if (coercedNumber >= 0) {
  583. value = Math.floor(coercedNumber);
  584. } else {
  585. value = Math.ceil(coercedNumber);
  586. }
  587. }
  588. return value;
  589. }
  590. function daysInMonth(year, month) {
  591. return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
  592. }
  593. function weeksInYear(year, dow, doy) {
  594. return weekOfYear(moment([year, 11, 31 + dow - doy]), dow, doy).week;
  595. }
  596. function daysInYear(year) {
  597. return isLeapYear(year) ? 366 : 365;
  598. }
  599. function isLeapYear(year) {
  600. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  601. }
  602. function checkOverflow(m) {
  603. var overflow;
  604. if (m._a && m._pf.overflow === -2) {
  605. overflow =
  606. m._a[MONTH] < 0 || m._a[MONTH] > 11 ? MONTH :
  607. m._a[DATE] < 1 || m._a[DATE] > daysInMonth(m._a[YEAR], m._a[MONTH]) ? DATE :
  608. m._a[HOUR] < 0 || m._a[HOUR] > 23 ? HOUR :
  609. m._a[MINUTE] < 0 || m._a[MINUTE] > 59 ? MINUTE :
  610. m._a[SECOND] < 0 || m._a[SECOND] > 59 ? SECOND :
  611. m._a[MILLISECOND] < 0 || m._a[MILLISECOND] > 999 ? MILLISECOND :
  612. -1;
  613. if (m._pf._overflowDayOfYear && (overflow < YEAR || overflow > DATE)) {
  614. overflow = DATE;
  615. }
  616. m._pf.overflow = overflow;
  617. }
  618. }
  619. function isValid(m) {
  620. if (m._isValid == null) {
  621. m._isValid = !isNaN(m._d.getTime()) &&
  622. m._pf.overflow < 0 &&
  623. !m._pf.empty &&
  624. !m._pf.invalidMonth &&
  625. !m._pf.nullInput &&
  626. !m._pf.invalidFormat &&
  627. !m._pf.userInvalidated;
  628. if (m._strict) {
  629. m._isValid = m._isValid &&
  630. m._pf.charsLeftOver === 0 &&
  631. m._pf.unusedTokens.length === 0;
  632. }
  633. }
  634. return m._isValid;
  635. }
  636. function normalizeLocale(key) {
  637. return key ? key.toLowerCase().replace('_', '-') : key;
  638. }
  639. // pick the locale from the array
  640. // try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
  641. // substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
  642. function chooseLocale(names) {
  643. var i = 0, j, next, locale, split;
  644. while (i < names.length) {
  645. split = normalizeLocale(names[i]).split('-');
  646. j = split.length;
  647. next = normalizeLocale(names[i + 1]);
  648. next = next ? next.split('-') : null;
  649. while (j > 0) {
  650. locale = loadLocale(split.slice(0, j).join('-'));
  651. if (locale) {
  652. return locale;
  653. }
  654. if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
  655. //the next array item is better than a shallower substring of this one
  656. break;
  657. }
  658. j--;
  659. }
  660. i++;
  661. }
  662. return null;
  663. }
  664. function loadLocale(name) {
  665. var oldLocale = null;
  666. if (!locales[name] && hasModule) {
  667. try {
  668. oldLocale = moment.locale();
  669. require('./locale/' + name);
  670. // because defineLocale currently also sets the global locale, we want to undo that for lazy loaded locales
  671. moment.locale(oldLocale);
  672. } catch (e) { }
  673. }
  674. return locales[name];
  675. }
  676. // Return a moment from input, that is local/utc/zone equivalent to model.
  677. function makeAs(input, model) {
  678. return model._isUTC ? moment(input).zone(model._offset || 0) :
  679. moment(input).local();
  680. }
  681. /************************************
  682. Locale
  683. ************************************/
  684. extend(Locale.prototype, {
  685. set : function (config) {
  686. var prop, i;
  687. for (i in config) {
  688. prop = config[i];
  689. if (typeof prop === 'function') {
  690. this[i] = prop;
  691. } else {
  692. this['_' + i] = prop;
  693. }
  694. }
  695. },
  696. _months : 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
  697. months : function (m) {
  698. return this._months[m.month()];
  699. },
  700. _monthsShort : 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
  701. monthsShort : function (m) {
  702. return this._monthsShort[m.month()];
  703. },
  704. monthsParse : function (monthName) {
  705. var i, mom, regex;
  706. if (!this._monthsParse) {
  707. this._monthsParse = [];
  708. }
  709. for (i = 0; i < 12; i++) {
  710. // make the regex if we don't have it already
  711. if (!this._monthsParse[i]) {
  712. mom = moment.utc([2000, i]);
  713. regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
  714. this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
  715. }
  716. // test the regex
  717. if (this._monthsParse[i].test(monthName)) {
  718. return i;
  719. }
  720. }
  721. },
  722. _weekdays : 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
  723. weekdays : function (m) {
  724. return this._weekdays[m.day()];
  725. },
  726. _weekdaysShort : 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
  727. weekdaysShort : function (m) {
  728. return this._weekdaysShort[m.day()];
  729. },
  730. _weekdaysMin : 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
  731. weekdaysMin : function (m) {
  732. return this._weekdaysMin[m.day()];
  733. },
  734. weekdaysParse : function (weekdayName) {
  735. var i, mom, regex;
  736. if (!this._weekdaysParse) {
  737. this._weekdaysParse = [];
  738. }
  739. for (i = 0; i < 7; i++) {
  740. // make the regex if we don't have it already
  741. if (!this._weekdaysParse[i]) {
  742. mom = moment([2000, 1]).day(i);
  743. regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
  744. this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i');
  745. }
  746. // test the regex
  747. if (this._weekdaysParse[i].test(weekdayName)) {
  748. return i;
  749. }
  750. }
  751. },
  752. _longDateFormat : {
  753. LT : 'h:mm A',
  754. L : 'MM/DD/YYYY',
  755. LL : 'MMMM D, YYYY',
  756. LLL : 'MMMM D, YYYY LT',
  757. LLLL : 'dddd, MMMM D, YYYY LT'
  758. },
  759. longDateFormat : function (key) {
  760. var output = this._longDateFormat[key];
  761. if (!output && this._longDateFormat[key.toUpperCase()]) {
  762. output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
  763. return val.slice(1);
  764. });
  765. this._longDateFormat[key] = output;
  766. }
  767. return output;
  768. },
  769. isPM : function (input) {
  770. // IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays
  771. // Using charAt should be more compatible.
  772. return ((input + '').toLowerCase().charAt(0) === 'p');
  773. },
  774. _meridiemParse : /[ap]\.?m?\.?/i,
  775. meridiem : function (hours, minutes, isLower) {
  776. if (hours > 11) {
  777. return isLower ? 'pm' : 'PM';
  778. } else {
  779. return isLower ? 'am' : 'AM';
  780. }
  781. },
  782. _calendar : {
  783. sameDay : '[Today at] LT',
  784. nextDay : '[Tomorrow at] LT',
  785. nextWeek : 'dddd [at] LT',
  786. lastDay : '[Yesterday at] LT',
  787. lastWeek : '[Last] dddd [at] LT',
  788. sameElse : 'L'
  789. },
  790. calendar : function (key, mom) {
  791. var output = this._calendar[key];
  792. return typeof output === 'function' ? output.apply(mom) : output;
  793. },
  794. _relativeTime : {
  795. future : 'in %s',
  796. past : '%s ago',
  797. s : 'a few seconds',
  798. m : 'a minute',
  799. mm : '%d minutes',
  800. h : 'an hour',
  801. hh : '%d hours',
  802. d : 'a day',
  803. dd : '%d days',
  804. M : 'a month',
  805. MM : '%d months',
  806. y : 'a year',
  807. yy : '%d years'
  808. },
  809. relativeTime : function (number, withoutSuffix, string, isFuture) {
  810. var output = this._relativeTime[string];
  811. return (typeof output === 'function') ?
  812. output(number, withoutSuffix, string, isFuture) :
  813. output.replace(/%d/i, number);
  814. },
  815. pastFuture : function (diff, output) {
  816. var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
  817. return typeof format === 'function' ? format(output) : format.replace(/%s/i, output);
  818. },
  819. ordinal : function (number) {
  820. return this._ordinal.replace('%d', number);
  821. },
  822. _ordinal : '%d',
  823. preparse : function (string) {
  824. return string;
  825. },
  826. postformat : function (string) {
  827. return string;
  828. },
  829. week : function (mom) {
  830. return weekOfYear(mom, this._week.dow, this._week.doy).week;
  831. },
  832. _week : {
  833. dow : 0, // Sunday is the first day of the week.
  834. doy : 6 // The week that contains Jan 1st is the first week of the year.
  835. },
  836. _invalidDate: 'Invalid date',
  837. invalidDate: function () {
  838. return this._invalidDate;
  839. }
  840. });
  841. /************************************
  842. Formatting
  843. ************************************/
  844. function removeFormattingTokens(input) {
  845. if (input.match(/\[[\s\S]/)) {
  846. return input.replace(/^\[|\]$/g, '');
  847. }
  848. return input.replace(/\\/g, '');
  849. }
  850. function makeFormatFunction(format) {
  851. var array = format.match(formattingTokens), i, length;
  852. for (i = 0, length = array.length; i < length; i++) {
  853. if (formatTokenFunctions[array[i]]) {
  854. array[i] = formatTokenFunctions[array[i]];
  855. } else {
  856. array[i] = removeFormattingTokens(array[i]);
  857. }
  858. }
  859. return function (mom) {
  860. var output = '';
  861. for (i = 0; i < length; i++) {
  862. output += array[i] instanceof Function ? array[i].call(mom, format) : array[i];
  863. }
  864. return output;
  865. };
  866. }
  867. // format date using native date object
  868. function formatMoment(m, format) {
  869. if (!m.isValid()) {
  870. return m.localeData().invalidDate();
  871. }
  872. format = expandFormat(format, m.localeData());
  873. if (!formatFunctions[format]) {
  874. formatFunctions[format] = makeFormatFunction(format);
  875. }
  876. return formatFunctions[format](m);
  877. }
  878. function expandFormat(format, locale) {
  879. var i = 5;
  880. function replaceLongDateFormatTokens(input) {
  881. return locale.longDateFormat(input) || input;
  882. }
  883. localFormattingTokens.lastIndex = 0;
  884. while (i >= 0 && localFormattingTokens.test(format)) {
  885. format = format.replace(localFormattingTokens, replaceLongDateFormatTokens);
  886. localFormattingTokens.lastIndex = 0;
  887. i -= 1;
  888. }
  889. return format;
  890. }
  891. /************************************
  892. Parsing
  893. ************************************/
  894. // get the regex to find the next token
  895. function getParseRegexForToken(token, config) {
  896. var a, strict = config._strict;
  897. switch (token) {
  898. case 'Q':
  899. return parseTokenOneDigit;
  900. case 'DDDD':
  901. return parseTokenThreeDigits;
  902. case 'YYYY':
  903. case 'GGGG':
  904. case 'gggg':
  905. return strict ? parseTokenFourDigits : parseTokenOneToFourDigits;
  906. case 'Y':
  907. case 'G':
  908. case 'g':
  909. return parseTokenSignedNumber;
  910. case 'YYYYYY':
  911. case 'YYYYY':
  912. case 'GGGGG':
  913. case 'ggggg':
  914. return strict ? parseTokenSixDigits : parseTokenOneToSixDigits;
  915. case 'S':
  916. if (strict) {
  917. return parseTokenOneDigit;
  918. }
  919. /* falls through */
  920. case 'SS':
  921. if (strict) {
  922. return parseTokenTwoDigits;
  923. }
  924. /* falls through */
  925. case 'SSS':
  926. if (strict) {
  927. return parseTokenThreeDigits;
  928. }
  929. /* falls through */
  930. case 'DDD':
  931. return parseTokenOneToThreeDigits;
  932. case 'MMM':
  933. case 'MMMM':
  934. case 'dd':
  935. case 'ddd':
  936. case 'dddd':
  937. return parseTokenWord;
  938. case 'a':
  939. case 'A':
  940. return config._locale._meridiemParse;
  941. case 'X':
  942. return parseTokenTimestampMs;
  943. case 'Z':
  944. case 'ZZ':
  945. return parseTokenTimezone;
  946. case 'T':
  947. return parseTokenT;
  948. case 'SSSS':
  949. return parseTokenDigits;
  950. case 'MM':
  951. case 'DD':
  952. case 'YY':
  953. case 'GG':
  954. case 'gg':
  955. case 'HH':
  956. case 'hh':
  957. case 'mm':
  958. case 'ss':
  959. case 'ww':
  960. case 'WW':
  961. return strict ? parseTokenTwoDigits : parseTokenOneOrTwoDigits;
  962. case 'M':
  963. case 'D':
  964. case 'd':
  965. case 'H':
  966. case 'h':
  967. case 'm':
  968. case 's':
  969. case 'w':
  970. case 'W':
  971. case 'e':
  972. case 'E':
  973. return parseTokenOneOrTwoDigits;
  974. case 'Do':
  975. return parseTokenOrdinal;
  976. default :
  977. a = new RegExp(regexpEscape(unescapeFormat(token.replace('\\', '')), 'i'));
  978. return a;
  979. }
  980. }
  981. function timezoneMinutesFromString(string) {
  982. string = string || '';
  983. var possibleTzMatches = (string.match(parseTokenTimezone) || []),
  984. tzChunk = possibleTzMatches[possibleTzMatches.length - 1] || [],
  985. parts = (tzChunk + '').match(parseTimezoneChunker) || ['-', 0, 0],
  986. minutes = +(parts[1] * 60) + toInt(parts[2]);
  987. return parts[0] === '+' ? -minutes : minutes;
  988. }
  989. // function to convert string input to date
  990. function addTimeToArrayFromToken(token, input, config) {
  991. var a, datePartArray = config._a;
  992. switch (token) {
  993. // QUARTER
  994. case 'Q':
  995. if (input != null) {
  996. datePartArray[MONTH] = (toInt(input) - 1) * 3;
  997. }
  998. break;
  999. // MONTH
  1000. case 'M' : // fall through to MM
  1001. case 'MM' :
  1002. if (input != null) {
  1003. datePartArray[MONTH] = toInt(input) - 1;
  1004. }
  1005. break;
  1006. case 'MMM' : // fall through to MMMM
  1007. case 'MMMM' :
  1008. a = config._locale.monthsParse(input);
  1009. // if we didn't find a month name, mark the date as invalid.
  1010. if (a != null) {
  1011. datePartArray[MONTH] = a;
  1012. } else {
  1013. config._pf.invalidMonth = input;
  1014. }
  1015. break;
  1016. // DAY OF MONTH
  1017. case 'D' : // fall through to DD
  1018. case 'DD' :
  1019. if (input != null) {
  1020. datePartArray[DATE] = toInt(input);
  1021. }
  1022. break;
  1023. case 'Do' :
  1024. if (input != null) {
  1025. datePartArray[DATE] = toInt(parseInt(input, 10));
  1026. }
  1027. break;
  1028. // DAY OF YEAR
  1029. case 'DDD' : // fall through to DDDD
  1030. case 'DDDD' :
  1031. if (input != null) {
  1032. config._dayOfYear = toInt(input);
  1033. }
  1034. break;
  1035. // YEAR
  1036. case 'YY' :
  1037. datePartArray[YEAR] = moment.parseTwoDigitYear(input);
  1038. break;
  1039. case 'YYYY' :
  1040. case 'YYYYY' :
  1041. case 'YYYYYY' :
  1042. datePartArray[YEAR] = toInt(input);
  1043. break;
  1044. // AM / PM
  1045. case 'a' : // fall through to A
  1046. case 'A' :
  1047. config._isPm = config._locale.isPM(input);
  1048. break;
  1049. // 24 HOUR
  1050. case 'H' : // fall through to hh
  1051. case 'HH' : // fall through to hh
  1052. case 'h' : // fall through to hh
  1053. case 'hh' :
  1054. datePartArray[HOUR] = toInt(input);
  1055. break;
  1056. // MINUTE
  1057. case 'm' : // fall through to mm
  1058. case 'mm' :
  1059. datePartArray[MINUTE] = toInt(input);
  1060. break;
  1061. // SECOND
  1062. case 's' : // fall through to ss
  1063. case 'ss' :
  1064. datePartArray[SECOND] = toInt(input);
  1065. break;
  1066. // MILLISECOND
  1067. case 'S' :
  1068. case 'SS' :
  1069. case 'SSS' :
  1070. case 'SSSS' :
  1071. datePartArray[MILLISECOND] = toInt(('0.' + input) * 1000);
  1072. break;
  1073. // UNIX TIMESTAMP WITH MS
  1074. case 'X':
  1075. config._d = new Date(parseFloat(input) * 1000);
  1076. break;
  1077. // TIMEZONE
  1078. case 'Z' : // fall through to ZZ
  1079. case 'ZZ' :
  1080. config._useUTC = true;
  1081. config._tzm = timezoneMinutesFromString(input);
  1082. break;
  1083. // WEEKDAY - human
  1084. case 'dd':
  1085. case 'ddd':
  1086. case 'dddd':
  1087. a = config._locale.weekdaysParse(input);
  1088. // if we didn't get a weekday name, mark the date as invalid
  1089. if (a != null) {
  1090. config._w = config._w || {};
  1091. config._w['d'] = a;
  1092. } else {
  1093. config._pf.invalidWeekday = input;
  1094. }
  1095. break;
  1096. // WEEK, WEEK DAY - numeric
  1097. case 'w':
  1098. case 'ww':
  1099. case 'W':
  1100. case 'WW':
  1101. case 'd':
  1102. case 'e':
  1103. case 'E':
  1104. token = token.substr(0, 1);
  1105. /* falls through */
  1106. case 'gggg':
  1107. case 'GGGG':
  1108. case 'GGGGG':
  1109. token = token.substr(0, 2);
  1110. if (input) {
  1111. config._w = config._w || {};
  1112. config._w[token] = toInt(input);
  1113. }
  1114. break;
  1115. case 'gg':
  1116. case 'GG':
  1117. config._w = config._w || {};
  1118. config._w[token] = moment.parseTwoDigitYear(input);
  1119. }
  1120. }
  1121. function dayOfYearFromWeekInfo(config) {
  1122. var w, weekYear, week, weekday, dow, doy, temp;
  1123. w = config._w;
  1124. if (w.GG != null || w.W != null || w.E != null) {
  1125. dow = 1;
  1126. doy = 4;
  1127. // TODO: We need to take the current isoWeekYear, but that depends on
  1128. // how we interpret now (local, utc, fixed offset). So create
  1129. // a now version of current config (take local/utc/offset flags, and
  1130. // create now).
  1131. weekYear = dfl(w.GG, config._a[YEAR], weekOfYear(moment(), 1, 4).year);
  1132. week = dfl(w.W, 1);
  1133. weekday = dfl(w.E, 1);
  1134. } else {
  1135. dow = config._locale._week.dow;
  1136. doy = config._locale._week.doy;
  1137. weekYear = dfl(w.gg, config._a[YEAR], weekOfYear(moment(), dow, doy).year);
  1138. week = dfl(w.w, 1);
  1139. if (w.d != null) {
  1140. // weekday -- low day numbers are considered next week
  1141. weekday = w.d;
  1142. if (weekday < dow) {
  1143. ++week;
  1144. }
  1145. } else if (w.e != null) {
  1146. // local weekday -- counting starts from begining of week
  1147. weekday = w.e + dow;
  1148. } else {
  1149. // default to begining of week
  1150. weekday = dow;
  1151. }
  1152. }
  1153. temp = dayOfYearFromWeeks(weekYear, week, weekday, doy, dow);
  1154. config._a[YEAR] = temp.year;
  1155. config._dayOfYear = temp.dayOfYear;
  1156. }
  1157. // convert an array to a date.
  1158. // the array should mirror the parameters below
  1159. // note: all values past the year are optional and will default to the lowest possible value.
  1160. // [year, month, day , hour, minute, second, millisecond]
  1161. function dateFromConfig(config) {
  1162. var i, date, input = [], currentDate, yearToUse;
  1163. if (config._d) {
  1164. return;
  1165. }
  1166. currentDate = currentDateArray(config);
  1167. //compute day of the year from weeks and weekdays
  1168. if (config._w && config._a[DATE] == null && config._a[MONTH] == null) {
  1169. dayOfYearFromWeekInfo(config);
  1170. }
  1171. //if the day of the year is set, figure out what it is
  1172. if (config._dayOfYear) {
  1173. yearToUse = dfl(config._a[YEAR], currentDate[YEAR]);
  1174. if (config._dayOfYear > daysInYear(yearToUse)) {
  1175. config._pf._overflowDayOfYear = true;
  1176. }
  1177. date = makeUTCDate(yearToUse, 0, config._dayOfYear);
  1178. config._a[MONTH] = date.getUTCMonth();
  1179. config._a[DATE] = date.getUTCDate();
  1180. }
  1181. // Default to current date.
  1182. // * if no year, month, day of month are given, default to today
  1183. // * if day of month is given, default month and year
  1184. // * if month is given, default only year
  1185. // * if year is given, don't default anything
  1186. for (i = 0; i < 3 && config._a[i] == null; ++i) {
  1187. config._a[i] = input[i] = currentDate[i];
  1188. }
  1189. // Zero out whatever was not defaulted, including time
  1190. for (; i < 7; i++) {
  1191. config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
  1192. }
  1193. config._d = (config._useUTC ? makeUTCDate : makeDate).apply(null, input);
  1194. // Apply timezone offset from input. The actual zone can be changed
  1195. // with parseZone.
  1196. if (config._tzm != null) {
  1197. config._d.setUTCMinutes(config._d.getUTCMinutes() + config._tzm);
  1198. }
  1199. }
  1200. function dateFromObject(config) {
  1201. var normalizedInput;
  1202. if (config._d) {
  1203. return;
  1204. }
  1205. normalizedInput = normalizeObjectUnits(config._i);
  1206. config._a = [
  1207. normalizedInput.year,
  1208. normalizedInput.month,
  1209. normalizedInput.day,
  1210. normalizedInput.hour,
  1211. normalizedInput.minute,
  1212. normalizedInput.second,
  1213. normalizedInput.millisecond
  1214. ];
  1215. dateFromConfig(config);
  1216. }
  1217. function currentDateArray(config) {
  1218. var now = new Date();
  1219. if (config._useUTC) {
  1220. return [
  1221. now.getUTCFullYear(),
  1222. now.getUTCMonth(),
  1223. now.getUTCDate()
  1224. ];
  1225. } else {
  1226. return [now.getFullYear(), now.getMonth(), now.getDate()];
  1227. }
  1228. }
  1229. // date from string and format string
  1230. function makeDateFromStringAndFormat(config) {
  1231. if (config._f === moment.ISO_8601) {
  1232. parseISO(config);
  1233. return;
  1234. }
  1235. config._a = [];
  1236. config._pf.empty = true;
  1237. // This array is used to make a Date, either with `new Date` or `Date.UTC`
  1238. var string = '' + config._i,
  1239. i, parsedInput, tokens, token, skipped,
  1240. stringLength = string.length,
  1241. totalParsedInputLength = 0;
  1242. tokens = expandFormat(config._f, config._locale).match(formattingTokens) || [];
  1243. for (i = 0; i < tokens.length; i++) {
  1244. token = tokens[i];
  1245. parsedInput = (string.match(getParseRegexForToken(token, config)) || [])[0];
  1246. if (parsedInput) {
  1247. skipped = string.substr(0, string.indexOf(parsedInput));
  1248. if (skipped.length > 0) {
  1249. config._pf.unusedInput.push(skipped);
  1250. }
  1251. string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
  1252. totalParsedInputLength += parsedInput.length;
  1253. }
  1254. // don't parse if it's not a known token
  1255. if (formatTokenFunctions[token]) {
  1256. if (parsedInput) {
  1257. config._pf.empty = false;
  1258. }
  1259. else {
  1260. config._pf.unusedTokens.push(token);
  1261. }
  1262. addTimeToArrayFromToken(token, parsedInput, config);
  1263. }
  1264. else if (config._strict && !parsedInput) {
  1265. config._pf.unusedTokens.push(token);
  1266. }
  1267. }
  1268. // add remaining unparsed input length to the string
  1269. config._pf.charsLeftOver = stringLength - totalParsedInputLength;
  1270. if (string.length > 0) {
  1271. config._pf.unusedInput.push(string);
  1272. }
  1273. // handle am pm
  1274. if (config._isPm && config._a[HOUR] < 12) {
  1275. config._a[HOUR] += 12;
  1276. }
  1277. // if is 12 am, change hours to 0
  1278. if (config._isPm === false && config._a[HOUR] === 12) {
  1279. config._a[HOUR] = 0;
  1280. }
  1281. dateFromConfig(config);
  1282. checkOverflow(config);
  1283. }
  1284. function unescapeFormat(s) {
  1285. return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) {
  1286. return p1 || p2 || p3 || p4;
  1287. });
  1288. }
  1289. // Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
  1290. function regexpEscape(s) {
  1291. return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  1292. }
  1293. // date from string and array of format strings
  1294. function makeDateFromStringAndArray(config) {
  1295. var tempConfig,
  1296. bestMoment,
  1297. scoreToBeat,
  1298. i,
  1299. currentScore;
  1300. if (config._f.length === 0) {
  1301. config._pf.invalidFormat = true;
  1302. config._d = new Date(NaN);
  1303. return;
  1304. }
  1305. for (i = 0; i < config._f.length; i++) {
  1306. currentScore = 0;
  1307. tempConfig = copyConfig({}, config);
  1308. tempConfig._pf = defaultParsingFlags();
  1309. tempConfig._f = config._f[i];
  1310. makeDateFromStringAndFormat(tempConfig);
  1311. if (!isValid(tempConfig)) {
  1312. continue;
  1313. }
  1314. // if there is any input that was not parsed add a penalty for that format
  1315. currentScore += tempConfig._pf.charsLeftOver;
  1316. //or tokens
  1317. currentScore += tempConfig._pf.unusedTokens.length * 10;
  1318. tempConfig._pf.score = currentScore;
  1319. if (scoreToBeat == null || currentScore < scoreToBeat) {
  1320. scoreToBeat = currentScore;
  1321. bestMoment = tempConfig;
  1322. }
  1323. }
  1324. extend(config, bestMoment || tempConfig);
  1325. }
  1326. // date from iso format
  1327. function parseISO(config) {
  1328. var i, l,
  1329. string = config._i,
  1330. match = isoRegex.exec(string);
  1331. if (match) {
  1332. config._pf.iso = true;
  1333. for (i = 0, l = isoDates.length; i < l; i++) {
  1334. if (isoDates[i][1].exec(string)) {
  1335. // match[5] should be "T" or undefined
  1336. config._f = isoDates[i][0] + (match[6] || ' ');
  1337. break;
  1338. }
  1339. }
  1340. for (i = 0, l = isoTimes.length; i < l; i++) {
  1341. if (isoTimes[i][1].exec(string)) {
  1342. config._f += isoTimes[i][0];
  1343. break;
  1344. }
  1345. }
  1346. if (string.match(parseTokenTimezone)) {
  1347. config._f += 'Z';
  1348. }
  1349. makeDateFromStringAndFormat(config);
  1350. } else {
  1351. config._isValid = false;
  1352. }
  1353. }
  1354. // date from iso format or fallback
  1355. function makeDateFromString(config) {
  1356. parseISO(config);
  1357. if (config._isValid === false) {
  1358. delete config._isValid;
  1359. moment.createFromInputFallback(config);
  1360. }
  1361. }
  1362. function makeDateFromInput(config) {
  1363. var input = config._i, matched;
  1364. if (input === undefined) {
  1365. config._d = new Date();
  1366. } else if (isDate(input)) {
  1367. config._d = new Date(+input);
  1368. } else if ((matched = aspNetJsonRegex.exec(input)) !== null) {
  1369. config._d = new Date(+matched[1]);
  1370. } else if (typeof input === 'string') {
  1371. makeDateFromString(config);
  1372. } else if (isArray(input)) {
  1373. config._a = input.slice(0);
  1374. dateFromConfig(config);
  1375. } else if (typeof(input) === 'object') {
  1376. dateFromObject(config);
  1377. } else if (typeof(input) === 'number') {
  1378. // from milliseconds
  1379. config._d = new Date(input);
  1380. } else {
  1381. moment.createFromInputFallback(config);
  1382. }
  1383. }
  1384. function makeDate(y, m, d, h, M, s, ms) {
  1385. //can't just apply() to create a date:
  1386. //http://stackoverflow.com/questions/181348/instantiating-a-javascript-object-by-calling-prototype-constructor-apply
  1387. var date = new Date(y, m, d, h, M, s, ms);
  1388. //the date constructor doesn't accept years < 1970
  1389. if (y < 1970) {
  1390. date.setFullYear(y);
  1391. }
  1392. return date;
  1393. }
  1394. function makeUTCDate(y) {
  1395. var date = new Date(Date.UTC.apply(null, arguments));
  1396. if (y < 1970) {
  1397. date.setUTCFullYear(y);
  1398. }
  1399. return date;
  1400. }
  1401. function parseWeekday(input, locale) {
  1402. if (typeof input === 'string') {
  1403. if (!isNaN(input)) {
  1404. input = parseInt(input, 10);
  1405. }
  1406. else {
  1407. input = locale.weekdaysParse(input);
  1408. if (typeof input !== 'number') {
  1409. return null;
  1410. }
  1411. }
  1412. }
  1413. return input;
  1414. }
  1415. /************************************
  1416. Relative Time
  1417. ************************************/
  1418. // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
  1419. function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
  1420. return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
  1421. }
  1422. function relativeTime(posNegDuration, withoutSuffix, locale) {
  1423. var duration = moment.duration(posNegDuration).abs(),
  1424. seconds = round(duration.as('s')),
  1425. minutes = round(duration.as('m')),
  1426. hours = round(duration.as('h')),
  1427. days = round(duration.as('d')),
  1428. months = round(duration.as('M')),
  1429. years = round(duration.as('y')),
  1430. args = seconds < relativeTimeThresholds.s && ['s', seconds] ||
  1431. minutes === 1 && ['m'] ||
  1432. minutes < relativeTimeThresholds.m && ['mm', minutes] ||
  1433. hours === 1 && ['h'] ||
  1434. hours < relativeTimeThresholds.h && ['hh', hours] ||
  1435. days === 1 && ['d'] ||
  1436. days < relativeTimeThresholds.d && ['dd', days] ||
  1437. months === 1 && ['M'] ||
  1438. months < relativeTimeThresholds.M && ['MM', months] ||
  1439. years === 1 && ['y'] || ['yy', years];
  1440. args[2] = withoutSuffix;
  1441. args[3] = +posNegDuration > 0;
  1442. args[4] = locale;
  1443. return substituteTimeAgo.apply({}, args);
  1444. }
  1445. /************************************
  1446. Week of Year
  1447. ************************************/
  1448. // firstDayOfWeek 0 = sun, 6 = sat
  1449. // the day of the week that starts the week
  1450. // (usually sunday or monday)
  1451. // firstDayOfWeekOfYear 0 = sun, 6 = sat
  1452. // the first week is the week that contains the first
  1453. // of this day of the week
  1454. // (eg. ISO weeks use thursday (4))
  1455. function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
  1456. var end = firstDayOfWeekOfYear - firstDayOfWeek,
  1457. daysToDayOfWeek = firstDayOfWeekOfYear - mom.day(),
  1458. adjustedMoment;
  1459. if (daysToDayOfWeek > end) {
  1460. daysToDayOfWeek -= 7;
  1461. }
  1462. if (daysToDayOfWeek < end - 7) {
  1463. daysToDayOfWeek += 7;
  1464. }
  1465. adjustedMoment = moment(mom).add(daysToDayOfWeek, 'd');
  1466. return {
  1467. week: Math.ceil(adjustedMoment.dayOfYear() / 7),
  1468. year: adjustedMoment.year()
  1469. };
  1470. }
  1471. //http://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday
  1472. function dayOfYearFromWeeks(year, week, weekday, firstDayOfWeekOfYear, firstDayOfWeek) {
  1473. var d = makeUTCDate(year, 0, 1).getUTCDay(), daysToAdd, dayOfYear;
  1474. d = d === 0 ? 7 : d;
  1475. weekday = weekday != null ? weekday : firstDayOfWeek;
  1476. daysToAdd = firstDayOfWeek - d + (d > firstDayOfWeekOfYear ? 7 : 0) - (d < firstDayOfWeek ? 7 : 0);
  1477. dayOfYear = 7 * (week - 1) + (weekday - firstDayOfWeek) + daysToAdd + 1;
  1478. return {
  1479. year: dayOfYear > 0 ? year : year - 1,
  1480. dayOfYear: dayOfYear > 0 ? dayOfYear : daysInYear(year - 1) + dayOfYear
  1481. };
  1482. }
  1483. /************************************
  1484. Top Level Functions
  1485. ************************************/
  1486. function makeMoment(config) {
  1487. var input = config._i,
  1488. format = config._f;
  1489. config._locale = config._locale || moment.localeData(config._l);
  1490. if (input === null || (format === undefined && input === '')) {
  1491. return moment.invalid({nullInput: true});
  1492. }
  1493. if (typeof input === 'string') {
  1494. config._i = input = config._locale.preparse(input);
  1495. }
  1496. if (moment.isMoment(input)) {
  1497. return new Moment(input, true);
  1498. } else if (format) {
  1499. if (isArray(format)) {
  1500. makeDateFromStringAndArray(config);
  1501. } else {
  1502. makeDateFromStringAndFormat(config);
  1503. }
  1504. } else {
  1505. makeDateFromInput(config);
  1506. }
  1507. return new Moment(config);
  1508. }
  1509. moment = function (input, format, locale, strict) {
  1510. var c;
  1511. if (typeof(locale) === "boolean") {
  1512. strict = locale;
  1513. locale = undefined;
  1514. }
  1515. // object construction must be done this way.
  1516. // https://github.com/moment/moment/issues/1423
  1517. c = {};
  1518. c._isAMomentObject = true;
  1519. c._i = input;
  1520. c._f = format;
  1521. c._l = locale;
  1522. c._strict = strict;
  1523. c._isUTC = false;
  1524. c._pf = defaultParsingFlags();
  1525. return makeMoment(c);
  1526. };
  1527. moment.suppressDeprecationWarnings = false;
  1528. moment.createFromInputFallback = deprecate(
  1529. 'moment construction falls back to js Date. This is ' +
  1530. 'discouraged and will be removed in upcoming major ' +
  1531. 'release. Please refer to ' +
  1532. 'https://github.com/moment/moment/issues/1407 for more info.',
  1533. function (config) {
  1534. config._d = new Date(config._i);
  1535. }
  1536. );
  1537. // Pick a moment m from moments so that m[fn](other) is true for all
  1538. // other. This relies on the function fn to be transitive.
  1539. //
  1540. // moments should either be an array of moment objects or an array, whose
  1541. // first element is an array of moment objects.
  1542. function pickBy(fn, moments) {
  1543. var res, i;
  1544. if (moments.length === 1 && isArray(moments[0])) {
  1545. moments = moments[0];
  1546. }
  1547. if (!moments.length) {
  1548. return moment();
  1549. }
  1550. res = moments[0];
  1551. for (i = 1; i < moments.length; ++i) {
  1552. if (moments[i][fn](res)) {
  1553. res = moments[i];
  1554. }
  1555. }
  1556. return res;
  1557. }
  1558. moment.min = function () {
  1559. var args = [].slice.call(arguments, 0);
  1560. return pickBy('isBefore', args);
  1561. };
  1562. moment.max = function () {
  1563. var args = [].slice.call(arguments, 0);
  1564. return pickBy('isAfter', args);
  1565. };
  1566. // creating with utc
  1567. moment.utc = function (input, format, locale, strict) {
  1568. var c;
  1569. if (typeof(locale) === "boolean") {
  1570. strict = locale;
  1571. locale = undefined;
  1572. }
  1573. // object construction must be done this way.
  1574. // https://github.com/moment/moment/issues/1423
  1575. c = {};
  1576. c._isAMomentObject = true;
  1577. c._useUTC = true;
  1578. c._isUTC = true;
  1579. c._l = locale;
  1580. c._i = input;
  1581. c._f = format;
  1582. c._strict = strict;
  1583. c._pf = defaultParsingFlags();
  1584. return makeMoment(c).utc();
  1585. };
  1586. // creating with unix timestamp (in seconds)
  1587. moment.unix = function (input) {
  1588. return moment(input * 1000);
  1589. };
  1590. // duration
  1591. moment.duration = function (input, key) {
  1592. var duration = input,
  1593. // matching against regexp is expensive, do it on demand
  1594. match = null,
  1595. sign,
  1596. ret,
  1597. parseIso,
  1598. diffRes;
  1599. if (moment.isDuration(input)) {
  1600. duration = {
  1601. ms: input._milliseconds,
  1602. d: input._days,
  1603. M: input._months
  1604. };
  1605. } else if (typeof input === 'number') {
  1606. duration = {};
  1607. if (key) {
  1608. duration[key] = input;
  1609. } else {
  1610. duration.milliseconds = input;
  1611. }
  1612. } else if (!!(match = aspNetTimeSpanJsonRegex.exec(input))) {
  1613. sign = (match[1] === '-') ? -1 : 1;
  1614. duration = {
  1615. y: 0,
  1616. d: toInt(match[DATE]) * sign,
  1617. h: toInt(match[HOUR]) * sign,
  1618. m: toInt(match[MINUTE]) * sign,
  1619. s: toInt(match[SECOND]) * sign,
  1620. ms: toInt(match[MILLISECOND]) * sign
  1621. };
  1622. } else if (!!(match = isoDurationRegex.exec(input))) {
  1623. sign = (match[1] === '-') ? -1 : 1;
  1624. parseIso = function (inp) {
  1625. // We'd normally use ~~inp for this, but unfortunately it also
  1626. // converts floats to ints.
  1627. // inp may be undefined, so careful calling replace on it.
  1628. var res = inp && parseFloat(inp.replace(',', '.'));
  1629. // apply sign while we're at it
  1630. return (isNaN(res) ? 0 : res) * sign;
  1631. };
  1632. duration = {
  1633. y: parseIso(match[2]),
  1634. M: parseIso(match[3]),
  1635. d: parseIso(match[4]),
  1636. h: parseIso(match[5]),
  1637. m: parseIso(match[6]),
  1638. s: parseIso(match[7]),
  1639. w: parseIso(match[8])
  1640. };
  1641. } else if (typeof duration === 'object' &&
  1642. ('from' in duration || 'to' in duration)) {
  1643. diffRes = momentsDifference(moment(duration.from), moment(duration.to));
  1644. duration = {};
  1645. duration.ms = diffRes.milliseconds;
  1646. duration.M = diffRes.months;
  1647. }
  1648. ret = new Duration(duration);
  1649. if (moment.isDuration(input) && input.hasOwnProperty('_locale')) {
  1650. ret._locale = input._locale;
  1651. }
  1652. return ret;
  1653. };
  1654. // version number
  1655. moment.version = VERSION;
  1656. // default format
  1657. moment.defaultFormat = isoFormat;
  1658. // constant that refers to the ISO standard
  1659. moment.ISO_8601 = function () {};
  1660. // Plugins that add properties should also add the key here (null value),
  1661. // so we can properly clone ourselves.
  1662. moment.momentProperties = momentProperties;
  1663. // This function will be called whenever a moment is mutated.
  1664. // It is intended to keep the offset in sync with the timezone.
  1665. moment.updateOffset = function () {};
  1666. // This function allows you to set a threshold for relative time strings
  1667. moment.relativeTimeThreshold = function (threshold, limit) {
  1668. if (relativeTimeThresholds[threshold] === undefined) {
  1669. return false;
  1670. }
  1671. if (limit === undefined) {
  1672. return relativeTimeThresholds[threshold];
  1673. }
  1674. relativeTimeThresholds[threshold] = limit;
  1675. return true;
  1676. };
  1677. moment.lang = deprecate(
  1678. "moment.lang is deprecated. Use moment.locale instead.",
  1679. function (key, value) {
  1680. return moment.locale(key, value);
  1681. }
  1682. );
  1683. // This function will load locale and then set the global locale. If
  1684. // no arguments are passed in, it will simply return the current global
  1685. // locale key.
  1686. moment.locale = function (key, values) {
  1687. var data;
  1688. if (key) {
  1689. if (typeof(values) !== "undefined") {
  1690. data = moment.defineLocale(key, values);
  1691. }
  1692. else {
  1693. data = moment.localeData(key);
  1694. }
  1695. if (data) {
  1696. moment.duration._locale = moment._locale = data;
  1697. }
  1698. }
  1699. return moment._locale._abbr;
  1700. };
  1701. moment.defineLocale = function (name, values) {
  1702. if (values !== null) {
  1703. values.abbr = name;
  1704. if (!locales[name]) {
  1705. locales[name] = new Locale();
  1706. }
  1707. locales[name].set(values);
  1708. // backwards compat for now: also set the locale
  1709. moment.locale(name);
  1710. return locales[name];
  1711. } else {
  1712. // useful for testing
  1713. delete locales[name];
  1714. return null;
  1715. }
  1716. };
  1717. moment.langData = deprecate(
  1718. "moment.langData is deprecated. Use moment.localeData instead.",
  1719. function (key) {
  1720. return moment.localeData(key);
  1721. }
  1722. );
  1723. // returns locale data
  1724. moment.localeData = function (key) {
  1725. var locale;
  1726. if (key && key._locale && key._locale._abbr) {
  1727. key = key._locale._abbr;
  1728. }
  1729. if (!key) {
  1730. return moment._locale;
  1731. }
  1732. if (!isArray(key)) {
  1733. //short-circuit everything else
  1734. locale = loadLocale(key);
  1735. if (locale) {
  1736. return locale;
  1737. }
  1738. key = [key];
  1739. }
  1740. return chooseLocale(key);
  1741. };
  1742. // compare moment object
  1743. moment.isMoment = function (obj) {
  1744. return obj instanceof Moment ||
  1745. (obj != null && obj.hasOwnProperty('_isAMomentObject'));
  1746. };
  1747. // for typechecking Duration objects
  1748. moment.isDuration = function (obj) {
  1749. return obj instanceof Duration;
  1750. };
  1751. for (i = lists.length - 1; i >= 0; --i) {
  1752. makeList(lists[i]);
  1753. }
  1754. moment.normalizeUnits = function (units) {
  1755. return normalizeUnits(units);
  1756. };
  1757. moment.invalid = function (flags) {
  1758. var m = moment.utc(NaN);
  1759. if (flags != null) {
  1760. extend(m._pf, flags);
  1761. }
  1762. else {
  1763. m._pf.userInvalidated = true;
  1764. }
  1765. return m;
  1766. };
  1767. moment.parseZone = function () {
  1768. return moment.apply(null, arguments).parseZone();
  1769. };
  1770. moment.parseTwoDigitYear = function (input) {
  1771. return toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
  1772. };
  1773. /************************************
  1774. Moment Prototype
  1775. ************************************/
  1776. extend(moment.fn = Moment.prototype, {
  1777. clone : function () {
  1778. return moment(this);
  1779. },
  1780. valueOf : function () {
  1781. return +this._d + ((this._offset || 0) * 60000);
  1782. },
  1783. unix : function () {
  1784. return Math.floor(+this / 1000);
  1785. },
  1786. toString : function () {
  1787. return this.clone().locale('en').format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ");
  1788. },
  1789. toDate : function () {
  1790. return this._offset ? new Date(+this) : this._d;
  1791. },
  1792. toISOString : function () {
  1793. var m = moment(this).utc();
  1794. if (0 < m.year() && m.year() <= 9999) {
  1795. return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
  1796. } else {
  1797. return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
  1798. }
  1799. },
  1800. toArray : function () {
  1801. var m = this;
  1802. return [
  1803. m.year(),
  1804. m.month(),
  1805. m.date(),
  1806. m.hours(),
  1807. m.minutes(),
  1808. m.seconds(),
  1809. m.milliseconds()
  1810. ];
  1811. },
  1812. isValid : function () {
  1813. return isValid(this);
  1814. },
  1815. isDSTShifted : function () {
  1816. if (this._a) {
  1817. return this.isValid() && compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray()) > 0;
  1818. }
  1819. return false;
  1820. },
  1821. parsingFlags : function () {
  1822. return extend({}, this._pf);
  1823. },
  1824. invalidAt: function () {
  1825. return this._pf.overflow;
  1826. },
  1827. utc : function (keepLocalTime) {
  1828. return this.zone(0, keepLocalTime);
  1829. },
  1830. local : function (keepLocalTime) {
  1831. if (this._isUTC) {
  1832. this.zone(0, keepLocalTime);
  1833. this._isUTC = false;
  1834. if (keepLocalTime) {
  1835. this.add(this._d.getTimezoneOffset(), 'm');
  1836. }
  1837. }
  1838. return this;
  1839. },
  1840. format : function (inputString) {
  1841. var output = formatMoment(this, inputString || moment.defaultFormat);
  1842. return this.localeData().postformat(output);
  1843. },
  1844. add : createAdder(1, 'add'),
  1845. subtract : createAdder(-1, 'subtract'),
  1846. diff : function (input, units, asFloat) {
  1847. var that = makeAs(input, this),
  1848. zoneDiff = (this.zone() - that.zone()) * 6e4,
  1849. diff, output;
  1850. units = normalizeUnits(units);
  1851. if (units === 'year' || units === 'month') {
  1852. // average number of days in the months in the given dates
  1853. diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
  1854. // difference in months
  1855. output = ((this.year() - that.year()) * 12) + (this.month() - that.month());
  1856. // adjust by taking difference in days, average number of days
  1857. // and dst in the given months.
  1858. output += ((this - moment(this).startOf('month')) -
  1859. (that - moment(that).startOf('month'))) / diff;
  1860. // same as above but with zones, to negate all dst
  1861. output -= ((this.zone() - moment(this).startOf('month').zone()) -
  1862. (that.zone() - moment(that).startOf('month').zone())) * 6e4 / diff;
  1863. if (units === 'year') {
  1864. output = output / 12;
  1865. }
  1866. } else {
  1867. diff = (this - that);
  1868. output = units === 'second' ? diff / 1e3 : // 1000
  1869. units === 'minute' ? diff / 6e4 : // 1000 * 60
  1870. units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
  1871. units === 'day' ? (diff - zoneDiff) / 864e5 : // 1000 * 60 * 60 * 24, negate dst
  1872. units === 'week' ? (diff - zoneDiff) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst
  1873. diff;
  1874. }
  1875. return asFloat ? output : absRound(output);
  1876. },
  1877. from : function (time, withoutSuffix) {
  1878. return moment.duration({to: this, from: time}).locale(this.locale()).humanize(!withoutSuffix);
  1879. },
  1880. fromNow : function (withoutSuffix) {
  1881. return this.from(moment(), withoutSuffix);
  1882. },
  1883. calendar : function (time) {
  1884. // We want to compare the start of today, vs this.
  1885. // Getting start-of-today depends on whether we're zone'd or not.
  1886. var now = time || moment(),
  1887. sod = makeAs(now, this).startOf('day'),
  1888. diff = this.diff(sod, 'days', true),
  1889. format = diff < -6 ? 'sameElse' :
  1890. diff < -1 ? 'lastWeek' :
  1891. diff < 0 ? 'lastDay' :
  1892. diff < 1 ? 'sameDay' :
  1893. diff < 2 ? 'nextDay' :
  1894. diff < 7 ? 'nextWeek' : 'sameElse';
  1895. return this.format(this.localeData().calendar(format, this));
  1896. },
  1897. isLeapYear : function () {
  1898. return isLeapYear(this.year());
  1899. },
  1900. isDST : function () {
  1901. return (this.zone() < this.clone().month(0).zone() ||
  1902. this.zone() < this.clone().month(5).zone());
  1903. },
  1904. day : function (input) {
  1905. var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
  1906. if (input != null) {
  1907. input = parseWeekday(input, this.localeData());
  1908. return this.add(input - day, 'd');
  1909. } else {
  1910. return day;
  1911. }
  1912. },
  1913. month : makeAccessor('Month', true),
  1914. startOf : function (units) {
  1915. units = normalizeUnits(units);
  1916. // the following switch intentionally omits break keywords
  1917. // to utilize falling through the cases.
  1918. switch (units) {
  1919. case 'year':
  1920. this.month(0);
  1921. /* falls through */
  1922. case 'quarter':
  1923. case 'month':
  1924. this.date(1);
  1925. /* falls through */
  1926. case 'week':
  1927. case 'isoWeek':
  1928. case 'day':
  1929. this.hours(0);
  1930. /* falls through */
  1931. case 'hour':
  1932. this.minutes(0);
  1933. /* falls through */
  1934. case 'minute':
  1935. this.seconds(0);
  1936. /* falls through */
  1937. case 'second':
  1938. this.milliseconds(0);
  1939. /* falls through */
  1940. }
  1941. // weeks are a special case
  1942. if (units === 'week') {
  1943. this.weekday(0);
  1944. } else if (units === 'isoWeek') {
  1945. this.isoWeekday(1);
  1946. }
  1947. // quarters are also special
  1948. if (units === 'quarter') {
  1949. this.month(Math.floor(this.month() / 3) * 3);
  1950. }
  1951. return this;
  1952. },
  1953. endOf: function (units) {
  1954. units = normalizeUnits(units);
  1955. return this.startOf(units).add(1, (units === 'isoWeek' ? 'week' : units)).subtract(1, 'ms');
  1956. },
  1957. isAfter: function (input, units) {
  1958. units = typeof units !== 'undefined' ? units : 'millisecond';
  1959. return +this.clone().startOf(units) > +moment(input).startOf(units);
  1960. },
  1961. isBefore: function (input, units) {
  1962. units = typeof units !== 'undefined' ? units : 'millisecond';
  1963. return +this.clone().startOf(units) < +moment(input).startOf(units);
  1964. },
  1965. isSame: function (input, units) {
  1966. units = units || 'ms';
  1967. return +this.clone().startOf(units) === +makeAs(input, this).startOf(units);
  1968. },
  1969. min: deprecate(
  1970. 'moment().min is deprecated, use moment.min instead. https://github.com/moment/moment/issues/1548',
  1971. function (other) {
  1972. other = moment.apply(null, arguments);
  1973. return other < this ? this : other;
  1974. }
  1975. ),
  1976. max: deprecate(
  1977. 'moment().max is deprecated, use moment.max instead. https://github.com/moment/moment/issues/1548',
  1978. function (other) {
  1979. other = moment.apply(null, arguments);
  1980. return other > this ? this : other;
  1981. }
  1982. ),
  1983. // keepLocalTime = true means only change the timezone, without
  1984. // affecting the local hour. So 5:31:26 +0300 --[zone(2, true)]-->
  1985. // 5:31:26 +0200 It is possible that 5:31:26 doesn't exist int zone
  1986. // +0200, so we adjust the time as needed, to be valid.
  1987. //
  1988. // Keeping the time actually adds/subtracts (one hour)
  1989. // from the actual represented time. That is why we call updateOffset
  1990. // a second time. In case it wants us to change the offset again
  1991. // _changeInProgress == true case, then we have to adjust, because
  1992. // there is no such time in the given timezone.
  1993. zone : function (input, keepLocalTime) {
  1994. var offset = this._offset || 0,
  1995. localAdjust;
  1996. if (input != null) {
  1997. if (typeof input === 'string') {
  1998. input = timezoneMinutesFromString(input);
  1999. }
  2000. if (Math.abs(input) < 16) {
  2001. input = input * 60;
  2002. }
  2003. if (!this._isUTC && keepLocalTime) {
  2004. localAdjust = this._d.getTimezoneOffset();
  2005. }
  2006. this._offset = input;
  2007. this._isUTC = true;
  2008. if (localAdjust != null) {
  2009. this.subtract(localAdjust, 'm');
  2010. }
  2011. if (offset !== input) {
  2012. if (!keepLocalTime || this._changeInProgress) {
  2013. addOrSubtractDurationFromMoment(this,
  2014. moment.duration(offset - input, 'm'), 1, false);
  2015. } else if (!this._changeInProgress) {
  2016. this._changeInProgress = true;
  2017. moment.updateOffset(this, true);
  2018. this._changeInProgress = null;
  2019. }
  2020. }
  2021. } else {
  2022. return this._isUTC ? offset : this._d.getTimezoneOffset();
  2023. }
  2024. return this;
  2025. },
  2026. zoneAbbr : function () {
  2027. return this._isUTC ? 'UTC' : '';
  2028. },
  2029. zoneName : function () {
  2030. return this._isUTC ? 'Coordinated Universal Time' : '';
  2031. },
  2032. parseZone : function () {
  2033. if (this._tzm) {
  2034. this.zone(this._tzm);
  2035. } else if (typeof this._i === 'string') {
  2036. this.zone(this._i);
  2037. }
  2038. return this;
  2039. },
  2040. hasAlignedHourOffset : function (input) {
  2041. if (!input) {
  2042. input = 0;
  2043. }
  2044. else {
  2045. input = moment(input).zone();
  2046. }
  2047. return (this.zone() - input) % 60 === 0;
  2048. },
  2049. daysInMonth : function () {
  2050. return daysInMonth(this.year(), this.month());
  2051. },
  2052. dayOfYear : function (input) {
  2053. var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
  2054. return input == null ? dayOfYear : this.add((input - dayOfYear), 'd');
  2055. },
  2056. quarter : function (input) {
  2057. return input == null ? Math.ceil((this.month() + 1) / 3) : this.month((input - 1) * 3 + this.month() % 3);
  2058. },
  2059. weekYear : function (input) {
  2060. var year = weekOfYear(this, this.localeData()._week.dow, this.localeData()._week.doy).year;
  2061. return input == null ? year : this.add((input - year), 'y');
  2062. },
  2063. isoWeekYear : function (input) {
  2064. var year = weekOfYear(this, 1, 4).year;
  2065. return input == null ? year : this.add((input - year), 'y');
  2066. },
  2067. week : function (input) {
  2068. var week = this.localeData().week(this);
  2069. return input == null ? week : this.add((input - week) * 7, 'd');
  2070. },
  2071. isoWeek : function (input) {
  2072. var week = weekOfYear(this, 1, 4).week;
  2073. return input == null ? week : this.add((input - week) * 7, 'd');
  2074. },
  2075. weekday : function (input) {
  2076. var weekday = (this.day() + 7 - this.localeData()._week.dow) % 7;
  2077. return input == null ? weekday : this.add(input - weekday, 'd');
  2078. },
  2079. isoWeekday : function (input) {
  2080. // behaves the same as moment#day except
  2081. // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
  2082. // as a setter, sunday should belong to the previous week.
  2083. return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);
  2084. },
  2085. isoWeeksInYear : function () {
  2086. return weeksInYear(this.year(), 1, 4);
  2087. },
  2088. weeksInYear : function () {
  2089. var weekInfo = this.localeData()._week;
  2090. return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy);
  2091. },
  2092. get : function (units) {
  2093. units = normalizeUnits(units);
  2094. return this[units]();
  2095. },
  2096. set : function (units, value) {
  2097. units = normalizeUnits(units);
  2098. if (typeof this[units] === 'function') {
  2099. this[units](value);
  2100. }
  2101. return this;
  2102. },
  2103. // If passed a locale key, it will set the locale for this
  2104. // instance. Otherwise, it will return the locale configuration
  2105. // variables for this instance.
  2106. locale : function (key) {
  2107. if (key === undefined) {
  2108. return this._locale._abbr;
  2109. } else {
  2110. this._locale = moment.localeData(key);
  2111. return this;
  2112. }
  2113. },
  2114. lang : deprecate(
  2115. "moment().lang() is deprecated. Use moment().localeData() instead.",
  2116. function (key) {
  2117. if (key === undefined) {
  2118. return this.localeData();
  2119. } else {
  2120. this._locale = moment.localeData(key);
  2121. return this;
  2122. }
  2123. }
  2124. ),
  2125. localeData : function () {
  2126. return this._locale;
  2127. }
  2128. });
  2129. function rawMonthSetter(mom, value) {
  2130. var dayOfMonth;
  2131. // TODO: Move this out of here!
  2132. if (typeof value === 'string') {
  2133. value = mom.localeData().monthsParse(value);
  2134. // TODO: Another silent failure?
  2135. if (typeof value !== 'number') {
  2136. return mom;
  2137. }
  2138. }
  2139. dayOfMonth = Math.min(mom.date(),
  2140. daysInMonth(mom.year(), value));
  2141. mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth);
  2142. return mom;
  2143. }
  2144. function rawGetter(mom, unit) {
  2145. return mom._d['get' + (mom._isUTC ? 'UTC' : '') + unit]();
  2146. }
  2147. function rawSetter(mom, unit, value) {
  2148. if (unit === 'Month') {
  2149. return rawMonthSetter(mom, value);
  2150. } else {
  2151. return mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
  2152. }
  2153. }
  2154. function makeAccessor(unit, keepTime) {
  2155. return function (value) {
  2156. if (value != null) {
  2157. rawSetter(this, unit, value);
  2158. moment.updateOffset(this, keepTime);
  2159. return this;
  2160. } else {
  2161. return rawGetter(this, unit);
  2162. }
  2163. };
  2164. }
  2165. moment.fn.millisecond = moment.fn.milliseconds = makeAccessor('Milliseconds', false);
  2166. moment.fn.second = moment.fn.seconds = makeAccessor('Seconds', false);
  2167. moment.fn.minute = moment.fn.minutes = makeAccessor('Minutes', false);
  2168. // Setting the hour should keep the time, because the user explicitly
  2169. // specified which hour he wants. So trying to maintain the same hour (in
  2170. // a new timezone) makes sense. Adding/subtracting hours does not follow
  2171. // this rule.
  2172. moment.fn.hour = moment.fn.hours = makeAccessor('Hours', true);
  2173. // moment.fn.month is defined separately
  2174. moment.fn.date = makeAccessor('Date', true);
  2175. moment.fn.dates = deprecate('dates accessor is deprecated. Use date instead.', makeAccessor('Date', true));
  2176. moment.fn.year = makeAccessor('FullYear', true);
  2177. moment.fn.years = deprecate('years accessor is deprecated. Use year instead.', makeAccessor('FullYear', true));
  2178. // add plural methods
  2179. moment.fn.days = moment.fn.day;
  2180. moment.fn.months = moment.fn.month;
  2181. moment.fn.weeks = moment.fn.week;
  2182. moment.fn.isoWeeks = moment.fn.isoWeek;
  2183. moment.fn.quarters = moment.fn.quarter;
  2184. // add aliased format methods
  2185. moment.fn.toJSON = moment.fn.toISOString;
  2186. /************************************
  2187. Duration Prototype
  2188. ************************************/
  2189. function daysToYears (days) {
  2190. // 400 years have 146097 days (taking into account leap year rules)
  2191. return days * 400 / 146097;
  2192. }
  2193. function yearsToDays (years) {
  2194. // years * 365 + absRound(years / 4) -
  2195. // absRound(years / 100) + absRound(years / 400);
  2196. return years * 146097 / 400;
  2197. }
  2198. extend(moment.duration.fn = Duration.prototype, {
  2199. _bubble : function () {
  2200. var milliseconds = this._milliseconds,
  2201. days = this._days,
  2202. months = this._months,
  2203. data = this._data,
  2204. seconds, minutes, hours, years = 0;
  2205. // The following code bubbles up values, see the tests for
  2206. // examples of what that means.
  2207. data.milliseconds = milliseconds % 1000;
  2208. seconds = absRound(milliseconds / 1000);
  2209. data.seconds = seconds % 60;
  2210. minutes = absRound(seconds / 60);
  2211. data.minutes = minutes % 60;
  2212. hours = absRound(minutes / 60);
  2213. data.hours = hours % 24;
  2214. days += absRound(hours / 24);
  2215. // Accurately convert days to years, assume start from year 0.
  2216. years = absRound(daysToYears(days));
  2217. days -= absRound(yearsToDays(years));
  2218. // 30 days to a month
  2219. // TODO (iskren): Use anchor date (like 1st Jan) to compute this.
  2220. months += absRound(days / 30);
  2221. days %= 30;
  2222. // 12 months -> 1 year
  2223. years += absRound(months / 12);
  2224. months %= 12;
  2225. data.days = days;
  2226. data.months = months;
  2227. data.years = years;
  2228. },
  2229. abs : function () {
  2230. this._milliseconds = Math.abs(this._milliseconds);
  2231. this._days = Math.abs(this._days);
  2232. this._months = Math.abs(this._months);
  2233. this._data.milliseconds = Math.abs(this._data.milliseconds);
  2234. this._data.seconds = Math.abs(this._data.seconds);
  2235. this._data.minutes = Math.abs(this._data.minutes);
  2236. this._data.hours = Math.abs(this._data.hours);
  2237. this._data.months = Math.abs(this._data.months);
  2238. this._data.years = Math.abs(this._data.years);
  2239. return this;
  2240. },
  2241. weeks : function () {
  2242. return absRound(this.days() / 7);
  2243. },
  2244. valueOf : function () {
  2245. return this._milliseconds +
  2246. this._days * 864e5 +
  2247. (this._months % 12) * 2592e6 +
  2248. toInt(this._months / 12) * 31536e6;
  2249. },
  2250. humanize : function (withSuffix) {
  2251. var output = relativeTime(this, !withSuffix, this.localeData());
  2252. if (withSuffix) {
  2253. output = this.localeData().pastFuture(+this, output);
  2254. }
  2255. return this.localeData().postformat(output);
  2256. },
  2257. add : function (input, val) {
  2258. // supports only 2.0-style add(1, 's') or add(moment)
  2259. var dur = moment.duration(input, val);
  2260. this._milliseconds += dur._milliseconds;
  2261. this._days += dur._days;
  2262. this._months += dur._months;
  2263. this._bubble();
  2264. return this;
  2265. },
  2266. subtract : function (input, val) {
  2267. var dur = moment.duration(input, val);
  2268. this._milliseconds -= dur._milliseconds;
  2269. this._days -= dur._days;
  2270. this._months -= dur._months;
  2271. this._bubble();
  2272. return this;
  2273. },
  2274. get : function (units) {
  2275. units = normalizeUnits(units);
  2276. return this[units.toLowerCase() + 's']();
  2277. },
  2278. as : function (units) {
  2279. var days, months;
  2280. units = normalizeUnits(units);
  2281. days = this._days + this._milliseconds / 864e5;
  2282. if (units === 'month' || units === 'year') {
  2283. months = this._months + daysToYears(days) * 12;
  2284. return units === 'month' ? months : months / 12;
  2285. } else {
  2286. days += yearsToDays(this._months / 12);
  2287. switch (units) {
  2288. case 'week': return days / 7;
  2289. case 'day': return days;
  2290. case 'hour': return days * 24;
  2291. case 'minute': return days * 24 * 60;
  2292. case 'second': return days * 24 * 60 * 60;
  2293. case 'millisecond': return days * 24 * 60 * 60 * 1000;
  2294. default: throw new Error('Unknown unit ' + units);
  2295. }
  2296. }
  2297. },
  2298. lang : moment.fn.lang,
  2299. locale : moment.fn.locale,
  2300. toIsoString : deprecate(
  2301. "toIsoString() is deprecated. Please use toISOString() instead " +
  2302. "(notice the capitals)",
  2303. function () {
  2304. return this.toISOString();
  2305. }
  2306. ),
  2307. toISOString : function () {
  2308. // inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js
  2309. var years = Math.abs(this.years()),
  2310. months = Math.abs(this.months()),
  2311. days = Math.abs(this.days()),
  2312. hours = Math.abs(this.hours()),
  2313. minutes = Math.abs(this.minutes()),
  2314. seconds = Math.abs(this.seconds() + this.milliseconds() / 1000);
  2315. if (!this.asSeconds()) {
  2316. // this is the same as C#'s (Noda) and python (isodate)...
  2317. // but not other JS (goog.date)
  2318. return 'P0D';
  2319. }
  2320. return (this.asSeconds() < 0 ? '-' : '') +
  2321. 'P' +
  2322. (years ? years + 'Y' : '') +
  2323. (months ? months + 'M' : '') +
  2324. (days ? days + 'D' : '') +
  2325. ((hours || minutes || seconds) ? 'T' : '') +
  2326. (hours ? hours + 'H' : '') +
  2327. (minutes ? minutes + 'M' : '') +
  2328. (seconds ? seconds + 'S' : '');
  2329. },
  2330. localeData : function () {
  2331. return this._locale;
  2332. }
  2333. });
  2334. function makeDurationGetter(name) {
  2335. moment.duration.fn[name] = function () {
  2336. return this._data[name];
  2337. };
  2338. }
  2339. for (i in unitMillisecondFactors) {
  2340. if (unitMillisecondFactors.hasOwnProperty(i)) {
  2341. makeDurationGetter(i.toLowerCase());
  2342. }
  2343. }
  2344. moment.duration.fn.asMilliseconds = function () {
  2345. return this.as('ms');
  2346. };
  2347. moment.duration.fn.asSeconds = function () {
  2348. return this.as('s');
  2349. };
  2350. moment.duration.fn.asMinutes = function () {
  2351. return this.as('m');
  2352. };
  2353. moment.duration.fn.asHours = function () {
  2354. return this.as('h');
  2355. };
  2356. moment.duration.fn.asDays = function () {
  2357. return this.as('d');
  2358. };
  2359. moment.duration.fn.asWeeks = function () {
  2360. return this.as('weeks');
  2361. };
  2362. moment.duration.fn.asMonths = function () {
  2363. return this.as('M');
  2364. };
  2365. moment.duration.fn.asYears = function () {
  2366. return this.as('y');
  2367. };
  2368. /************************************
  2369. Default Locale
  2370. ************************************/
  2371. // Set default locale, other locale will inherit from English.
  2372. moment.locale('en', {
  2373. ordinal : function (number) {
  2374. var b = number % 10,
  2375. output = (toInt(number % 100 / 10) === 1) ? 'th' :
  2376. (b === 1) ? 'st' :
  2377. (b === 2) ? 'nd' :
  2378. (b === 3) ? 'rd' : 'th';
  2379. return number + output;
  2380. }
  2381. });
  2382. /* EMBED_LOCALES */
  2383. /************************************
  2384. Exposing Moment
  2385. ************************************/
  2386. function makeGlobal(shouldDeprecate) {
  2387. /*global ender:false */
  2388. if (typeof ender !== 'undefined') {
  2389. return;
  2390. }
  2391. oldGlobalMoment = globalScope.moment;
  2392. if (shouldDeprecate) {
  2393. globalScope.moment = deprecate(
  2394. 'Accessing Moment through the global scope is ' +
  2395. 'deprecated, and will be removed in an upcoming ' +
  2396. 'release.',
  2397. moment);
  2398. } else {
  2399. globalScope.moment = moment;
  2400. }
  2401. }
  2402. // CommonJS module is defined
  2403. if (hasModule) {
  2404. module.exports = moment;
  2405. } else if (typeof define === 'function' && define.amd) {
  2406. define('moment', function (require, exports, module) {
  2407. if (module.config && module.config() && module.config().noGlobal === true) {
  2408. // release the global variable
  2409. globalScope.moment = oldGlobalMoment;
  2410. }
  2411. return moment;
  2412. });
  2413. makeGlobal(true);
  2414. } else {
  2415. makeGlobal();
  2416. }
  2417. }).call(this);