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.

2807 lines
89 KiB

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