3 //! authors : Tim Wood, Iskren Chernev, Moment.js contributors
7 (function (undefined) {
9 /************************************
11 ************************************/
15 // the global-scope this is NOT the global object in Node.js
16 globalScope = typeof global !== 'undefined' ? global : this,
29 // internal storage for language config files
32 // moment internal properties
34 _isAMomentObject: null,
41 _offset : null, // optional. Combine with _isUTC
43 _lang : null // optional
47 hasModule = (typeof module !== 'undefined' && module.exports),
49 // ASP.NET json date format regex
50 aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
51 aspNetTimeSpanJsonRegex = /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/,
53 // from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html
54 // somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere
55 isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,
58 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,
59 localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
61 // parsing token regexes
62 parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99
63 parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999
64 parseTokenOneToFourDigits = /\d{1,4}/, // 0 - 9999
65 parseTokenOneToSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999
66 parseTokenDigits = /\d+/, // nonzero number of digits
67 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.
68 parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/gi, // +00:00 -00:00 +0000 -0000 or Z
69 parseTokenT = /T/i, // T (ISO separator)
70 parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
71 parseTokenOrdinal = /\d{1,2}/,
73 //strict parsing regexes
74 parseTokenOneDigit = /\d/, // 0 - 9
75 parseTokenTwoDigits = /\d\d/, // 00 - 99
76 parseTokenThreeDigits = /\d{3}/, // 000 - 999
77 parseTokenFourDigits = /\d{4}/, // 0000 - 9999
78 parseTokenSixDigits = /[+-]?\d{6}/, // -999,999 - 999,999
79 parseTokenSignedNumber = /[+-]?\d+/, // -inf - inf
82 // 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)
83 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)?)?$/,
85 isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
88 ['YYYYYY-MM-DD', /[+-]\d{6}-\d{2}-\d{2}/],
89 ['YYYY-MM-DD', /\d{4}-\d{2}-\d{2}/],
90 ['GGGG-[W]WW-E', /\d{4}-W\d{2}-\d/],
91 ['GGGG-[W]WW', /\d{4}-W\d{2}/],
92 ['YYYY-DDD', /\d{4}-\d{3}/]
95 // iso time formats and regexes
97 ['HH:mm:ss.SSSS', /(T| )\d\d:\d\d:\d\d\.\d+/],
98 ['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/],
99 ['HH:mm', /(T| )\d\d:\d\d/],
103 // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
104 parseTimezoneChunker = /([\+\-]|\d\d)/gi,
106 // getter and setter names
107 proxyGettersAndSetters = 'Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
108 unitMillisecondFactors = {
138 dayofyear : 'dayOfYear',
139 isoweekday : 'isoWeekday',
141 weekyear : 'weekYear',
142 isoweekyear : 'isoWeekYear'
145 // format function strings
146 formatFunctions = {},
148 // default relative time thresholds
149 relativeTimeThresholds = {
150 s: 45, //seconds to minutes
151 m: 45, //minutes to hours
152 h: 22, //hours to days
153 dd: 25, //days to month (month == 1)
154 dm: 45, //days to months (months > 1)
155 dy: 345 //days to year
158 // tokens to ordinalize and pad
159 ordinalizeTokens = 'DDD w W M D d'.split(' '),
160 paddedTokens = 'M D H h m s w W'.split(' '),
162 formatTokenFunctions = {
164 return this.month() + 1;
166 MMM : function (format) {
167 return this.lang().monthsShort(this, format);
169 MMMM : function (format) {
170 return this.lang().months(this, format);
176 return this.dayOfYear();
181 dd : function (format) {
182 return this.lang().weekdaysMin(this, format);
184 ddd : function (format) {
185 return this.lang().weekdaysShort(this, format);
187 dddd : function (format) {
188 return this.lang().weekdays(this, format);
194 return this.isoWeek();
197 return leftZeroFill(this.year() % 100, 2);
200 return leftZeroFill(this.year(), 4);
202 YYYYY : function () {
203 return leftZeroFill(this.year(), 5);
205 YYYYYY : function () {
206 var y = this.year(), sign = y >= 0 ? '+' : '-';
207 return sign + leftZeroFill(Math.abs(y), 6);
210 return leftZeroFill(this.weekYear() % 100, 2);
213 return leftZeroFill(this.weekYear(), 4);
215 ggggg : function () {
216 return leftZeroFill(this.weekYear(), 5);
219 return leftZeroFill(this.isoWeekYear() % 100, 2);
222 return leftZeroFill(this.isoWeekYear(), 4);
224 GGGGG : function () {
225 return leftZeroFill(this.isoWeekYear(), 5);
228 return this.weekday();
231 return this.isoWeekday();
234 return this.lang().meridiem(this.hours(), this.minutes(), true);
237 return this.lang().meridiem(this.hours(), this.minutes(), false);
243 return this.hours() % 12 || 12;
246 return this.minutes();
249 return this.seconds();
252 return toInt(this.milliseconds() / 100);
255 return leftZeroFill(toInt(this.milliseconds() / 10), 2);
258 return leftZeroFill(this.milliseconds(), 3);
261 return leftZeroFill(this.milliseconds(), 3);
264 var a = -this.zone(),
270 return b + leftZeroFill(toInt(a / 60), 2) + ":" + leftZeroFill(toInt(a) % 60, 2);
273 var a = -this.zone(),
279 return b + leftZeroFill(toInt(a / 60), 2) + leftZeroFill(toInt(a) % 60, 2);
282 return this.zoneAbbr();
285 return this.zoneName();
291 return this.quarter();
295 lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin'];
297 // Pick the first defined of two or three arguments. dfl comes from
299 function dfl(a, b, c) {
300 switch (arguments.length) {
301 case 2: return a != null ? a : b;
302 case 3: return a != null ? a : b != null ? b : c;
303 default: throw new Error("Implement me");
307 function defaultParsingFlags() {
308 // We need to deep clone this object, and es5 standard is not very
318 invalidFormat : false,
319 userInvalidated : false,
324 function deprecate(msg, fn) {
325 var firstTime = true;
326 function printMsg() {
327 if (moment.suppressDeprecationWarnings === false &&
328 typeof console !== 'undefined' && console.warn) {
329 console.warn("Deprecation warning: " + msg);
332 return extend(function () {
337 return fn.apply(this, arguments);
341 function padToken(func, count) {
342 return function (a) {
343 return leftZeroFill(func.call(this, a), count);
346 function ordinalizeToken(func, period) {
347 return function (a) {
348 return this.lang().ordinal(func.call(this, a), period);
352 while (ordinalizeTokens.length) {
353 i = ordinalizeTokens.pop();
354 formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i], i);
356 while (paddedTokens.length) {
357 i = paddedTokens.pop();
358 formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2);
360 formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
363 /************************************
365 ************************************/
367 function Language() {
371 // Moment prototype object
372 function Moment(config) {
373 checkOverflow(config);
374 extend(this, config);
377 // Duration Constructor
378 function Duration(duration) {
379 var normalizedInput = normalizeObjectUnits(duration),
380 years = normalizedInput.year || 0,
381 quarters = normalizedInput.quarter || 0,
382 months = normalizedInput.month || 0,
383 weeks = normalizedInput.week || 0,
384 days = normalizedInput.day || 0,
385 hours = normalizedInput.hour || 0,
386 minutes = normalizedInput.minute || 0,
387 seconds = normalizedInput.second || 0,
388 milliseconds = normalizedInput.millisecond || 0;
390 // representation for dateAddRemove
391 this._milliseconds = +milliseconds +
392 seconds * 1e3 + // 1000
393 minutes * 6e4 + // 1000 * 60
394 hours * 36e5; // 1000 * 60 * 60
395 // Because of dateAddRemove treats 24 hours as different from a
396 // day when working around DST, we need to store them separately
399 // It is impossible translate months into days without knowing
400 // which months you are are talking about, so we have to store
402 this._months = +months +
411 /************************************
413 ************************************/
416 function extend(a, b) {
418 if (b.hasOwnProperty(i)) {
423 if (b.hasOwnProperty("toString")) {
424 a.toString = b.toString;
427 if (b.hasOwnProperty("valueOf")) {
428 a.valueOf = b.valueOf;
434 function cloneMoment(m) {
437 if (m.hasOwnProperty(i) && momentProperties.hasOwnProperty(i)) {
445 function absRound(number) {
447 return Math.ceil(number);
449 return Math.floor(number);
453 // left zero fill a number
454 // see http://jsperf.com/left-zero-filling for performance comparison
455 function leftZeroFill(number, targetLength, forceSign) {
456 var output = '' + Math.abs(number),
459 while (output.length < targetLength) {
460 output = '0' + output;
462 return (sign ? (forceSign ? '+' : '') : '-') + output;
465 // helper function for _.addTime and _.subtractTime
466 function addOrSubtractDurationFromMoment(mom, duration, isAdding, updateOffset) {
467 var milliseconds = duration._milliseconds,
468 days = duration._days,
469 months = duration._months;
470 updateOffset = updateOffset == null ? true : updateOffset;
473 mom._d.setTime(+mom._d + milliseconds * isAdding);
476 rawSetter(mom, 'Date', rawGetter(mom, 'Date') + days * isAdding);
479 rawMonthSetter(mom, rawGetter(mom, 'Month') + months * isAdding);
482 moment.updateOffset(mom, days || months);
486 // check if is an array
487 function isArray(input) {
488 return Object.prototype.toString.call(input) === '[object Array]';
491 function isDate(input) {
492 return Object.prototype.toString.call(input) === '[object Date]' ||
493 input instanceof Date;
496 // compare two arrays, return the number of differences
497 function compareArrays(array1, array2, dontConvert) {
498 var len = Math.min(array1.length, array2.length),
499 lengthDiff = Math.abs(array1.length - array2.length),
502 for (i = 0; i < len; i++) {
503 if ((dontConvert && array1[i] !== array2[i]) ||
504 (!dontConvert && toInt(array1[i]) !== toInt(array2[i]))) {
508 return diffs + lengthDiff;
511 function normalizeUnits(units) {
513 var lowered = units.toLowerCase().replace(/(.)s$/, '$1');
514 units = unitAliases[units] || camelFunctions[lowered] || lowered;
519 function normalizeObjectUnits(inputObject) {
520 var normalizedInput = {},
524 for (prop in inputObject) {
525 if (inputObject.hasOwnProperty(prop)) {
526 normalizedProp = normalizeUnits(prop);
527 if (normalizedProp) {
528 normalizedInput[normalizedProp] = inputObject[prop];
533 return normalizedInput;
536 function makeList(field) {
539 if (field.indexOf('week') === 0) {
543 else if (field.indexOf('month') === 0) {
551 moment[field] = function (format, index) {
553 method = moment.fn._lang[field],
556 if (typeof format === 'number') {
561 getter = function (i) {
562 var m = moment().utc().set(setter, i);
563 return method.call(moment.fn._lang, m, format || '');
567 return getter(index);
570 for (i = 0; i < count; i++) {
571 results.push(getter(i));
578 function toInt(argumentForCoercion) {
579 var coercedNumber = +argumentForCoercion,
582 if (coercedNumber !== 0 && isFinite(coercedNumber)) {
583 if (coercedNumber >= 0) {
584 value = Math.floor(coercedNumber);
586 value = Math.ceil(coercedNumber);
593 function daysInMonth(year, month) {
594 return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
597 function weeksInYear(year, dow, doy) {
598 return weekOfYear(moment([year, 11, 31 + dow - doy]), dow, doy).week;
601 function daysInYear(year) {
602 return isLeapYear(year) ? 366 : 365;
605 function isLeapYear(year) {
606 return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
609 function checkOverflow(m) {
611 if (m._a && m._pf.overflow === -2) {
613 m._a[MONTH] < 0 || m._a[MONTH] > 11 ? MONTH :
614 m._a[DATE] < 1 || m._a[DATE] > daysInMonth(m._a[YEAR], m._a[MONTH]) ? DATE :
615 m._a[HOUR] < 0 || m._a[HOUR] > 23 ? HOUR :
616 m._a[MINUTE] < 0 || m._a[MINUTE] > 59 ? MINUTE :
617 m._a[SECOND] < 0 || m._a[SECOND] > 59 ? SECOND :
618 m._a[MILLISECOND] < 0 || m._a[MILLISECOND] > 999 ? MILLISECOND :
621 if (m._pf._overflowDayOfYear && (overflow < YEAR || overflow > DATE)) {
625 m._pf.overflow = overflow;
629 function isValid(m) {
630 if (m._isValid == null) {
631 m._isValid = !isNaN(m._d.getTime()) &&
632 m._pf.overflow < 0 &&
634 !m._pf.invalidMonth &&
636 !m._pf.invalidFormat &&
637 !m._pf.userInvalidated;
640 m._isValid = m._isValid &&
641 m._pf.charsLeftOver === 0 &&
642 m._pf.unusedTokens.length === 0;
648 function normalizeLanguage(key) {
649 return key ? key.toLowerCase().replace('_', '-') : key;
652 // Return a moment from input, that is local/utc/zone equivalent to model.
653 function makeAs(input, model) {
654 return model._isUTC ? moment(input).zone(model._offset || 0) :
655 moment(input).local();
658 /************************************
660 ************************************/
663 extend(Language.prototype, {
665 set : function (config) {
669 if (typeof prop === 'function') {
672 this['_' + i] = prop;
677 _months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
678 months : function (m) {
679 return this._months[m.month()];
682 _monthsShort : "Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),
683 monthsShort : function (m) {
684 return this._monthsShort[m.month()];
687 monthsParse : function (monthName) {
690 if (!this._monthsParse) {
691 this._monthsParse = [];
694 for (i = 0; i < 12; i++) {
695 // make the regex if we don't have it already
696 if (!this._monthsParse[i]) {
697 mom = moment.utc([2000, i]);
698 regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
699 this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
702 if (this._monthsParse[i].test(monthName)) {
708 _weekdays : "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
709 weekdays : function (m) {
710 return this._weekdays[m.day()];
713 _weekdaysShort : "Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),
714 weekdaysShort : function (m) {
715 return this._weekdaysShort[m.day()];
718 _weekdaysMin : "Su_Mo_Tu_We_Th_Fr_Sa".split("_"),
719 weekdaysMin : function (m) {
720 return this._weekdaysMin[m.day()];
723 weekdaysParse : function (weekdayName) {
726 if (!this._weekdaysParse) {
727 this._weekdaysParse = [];
730 for (i = 0; i < 7; i++) {
731 // make the regex if we don't have it already
732 if (!this._weekdaysParse[i]) {
733 mom = moment([2000, 1]).day(i);
734 regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
735 this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i');
738 if (this._weekdaysParse[i].test(weekdayName)) {
748 LLL : "MMMM D YYYY LT",
749 LLLL : "dddd, MMMM D YYYY LT"
751 longDateFormat : function (key) {
752 var output = this._longDateFormat[key];
753 if (!output && this._longDateFormat[key.toUpperCase()]) {
754 output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
757 this._longDateFormat[key] = output;
762 isPM : function (input) {
763 // IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays
764 // Using charAt should be more compatible.
765 return ((input + '').toLowerCase().charAt(0) === 'p');
768 _meridiemParse : /[ap]\.?m?\.?/i,
769 meridiem : function (hours, minutes, isLower) {
771 return isLower ? 'pm' : 'PM';
773 return isLower ? 'am' : 'AM';
778 sameDay : '[Today at] LT',
779 nextDay : '[Tomorrow at] LT',
780 nextWeek : 'dddd [at] LT',
781 lastDay : '[Yesterday at] LT',
782 lastWeek : '[Last] dddd [at] LT',
785 calendar : function (key, mom) {
786 var output = this._calendar[key];
787 return typeof output === 'function' ? output.apply(mom) : output;
805 relativeTime : function (number, withoutSuffix, string, isFuture) {
806 var output = this._relativeTime[string];
807 return (typeof output === 'function') ?
808 output(number, withoutSuffix, string, isFuture) :
809 output.replace(/%d/i, number);
811 pastFuture : function (diff, output) {
812 var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
813 return typeof format === 'function' ? format(output) : format.replace(/%s/i, output);
816 ordinal : function (number) {
817 return this._ordinal.replace("%d", number);
821 preparse : function (string) {
825 postformat : function (string) {
829 week : function (mom) {
830 return weekOfYear(mom, this._week.dow, this._week.doy).week;
834 dow : 0, // Sunday is the first day of the week.
835 doy : 6 // The week that contains Jan 1st is the first week of the year.
838 _invalidDate: 'Invalid date',
839 invalidDate: function () {
840 return this._invalidDate;
844 // Loads a language definition into the `languages` cache. The function
845 // takes a key and optionally values. If not in the browser and no values
846 // are provided, it will load the language file module. As a convenience,
847 // this function also returns the language values.
848 function loadLang(key, values) {
850 if (!languages[key]) {
851 languages[key] = new Language();
853 languages[key].set(values);
854 return languages[key];
857 // Remove a language from the `languages` cache. Mostly useful in tests.
858 function unloadLang(key) {
859 delete languages[key];
862 // Determines which language definition to use and returns it.
864 // With no parameters, it will return the global language. If you
865 // pass in a language key, such as 'en', it will return the
866 // definition for 'en', so long as 'en' has already been loaded using
868 function getLangDefinition(key) {
869 var i = 0, j, lang, next, split,
871 if (!languages[k] && hasModule) {
873 require('./lang/' + k);
880 return moment.fn._lang;
884 //short-circuit everything else
892 //pick the language from the array
893 //try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
894 //substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
895 while (i < key.length) {
896 split = normalizeLanguage(key[i]).split('-');
898 next = normalizeLanguage(key[i + 1]);
899 next = next ? next.split('-') : null;
901 lang = get(split.slice(0, j).join('-'));
905 if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
906 //the next array item is better than a shallower substring of this one
913 return moment.fn._lang;
916 /************************************
918 ************************************/
921 function removeFormattingTokens(input) {
922 if (input.match(/\[[\s\S]/)) {
923 return input.replace(/^\[|\]$/g, "");
925 return input.replace(/\\/g, "");
928 function makeFormatFunction(format) {
929 var array = format.match(formattingTokens), i, length;
931 for (i = 0, length = array.length; i < length; i++) {
932 if (formatTokenFunctions[array[i]]) {
933 array[i] = formatTokenFunctions[array[i]];
935 array[i] = removeFormattingTokens(array[i]);
939 return function (mom) {
941 for (i = 0; i < length; i++) {
942 output += array[i] instanceof Function ? array[i].call(mom, format) : array[i];
948 // format date using native date object
949 function formatMoment(m, format) {
952 return m.lang().invalidDate();
955 format = expandFormat(format, m.lang());
957 if (!formatFunctions[format]) {
958 formatFunctions[format] = makeFormatFunction(format);
961 return formatFunctions[format](m);
964 function expandFormat(format, lang) {
967 function replaceLongDateFormatTokens(input) {
968 return lang.longDateFormat(input) || input;
971 localFormattingTokens.lastIndex = 0;
972 while (i >= 0 && localFormattingTokens.test(format)) {
973 format = format.replace(localFormattingTokens, replaceLongDateFormatTokens);
974 localFormattingTokens.lastIndex = 0;
982 /************************************
984 ************************************/
987 // get the regex to find the next token
988 function getParseRegexForToken(token, config) {
989 var a, strict = config._strict;
992 return parseTokenOneDigit;
994 return parseTokenThreeDigits;
998 return strict ? parseTokenFourDigits : parseTokenOneToFourDigits;
1002 return parseTokenSignedNumber;
1007 return strict ? parseTokenSixDigits : parseTokenOneToSixDigits;
1009 if (strict) { return parseTokenOneDigit; }
1012 if (strict) { return parseTokenTwoDigits; }
1015 if (strict) { return parseTokenThreeDigits; }
1018 return parseTokenOneToThreeDigits;
1024 return parseTokenWord;
1027 return getLangDefinition(config._l)._meridiemParse;
1029 return parseTokenTimestampMs;
1032 return parseTokenTimezone;
1036 return parseTokenDigits;
1048 return strict ? parseTokenTwoDigits : parseTokenOneOrTwoDigits;
1060 return parseTokenOneOrTwoDigits;
1062 return parseTokenOrdinal;
1064 a = new RegExp(regexpEscape(unescapeFormat(token.replace('\\', '')), "i"));
1069 function timezoneMinutesFromString(string) {
1070 string = string || "";
1071 var possibleTzMatches = (string.match(parseTokenTimezone) || []),
1072 tzChunk = possibleTzMatches[possibleTzMatches.length - 1] || [],
1073 parts = (tzChunk + '').match(parseTimezoneChunker) || ['-', 0, 0],
1074 minutes = +(parts[1] * 60) + toInt(parts[2]);
1076 return parts[0] === '+' ? -minutes : minutes;
1079 // function to convert string input to date
1080 function addTimeToArrayFromToken(token, input, config) {
1081 var a, datePartArray = config._a;
1086 if (input != null) {
1087 datePartArray[MONTH] = (toInt(input) - 1) * 3;
1091 case 'M' : // fall through to MM
1093 if (input != null) {
1094 datePartArray[MONTH] = toInt(input) - 1;
1097 case 'MMM' : // fall through to MMMM
1099 a = getLangDefinition(config._l).monthsParse(input);
1100 // if we didn't find a month name, mark the date as invalid.
1102 datePartArray[MONTH] = a;
1104 config._pf.invalidMonth = input;
1108 case 'D' : // fall through to DD
1110 if (input != null) {
1111 datePartArray[DATE] = toInt(input);
1115 if (input != null) {
1116 datePartArray[DATE] = toInt(parseInt(input, 10));
1120 case 'DDD' : // fall through to DDDD
1122 if (input != null) {
1123 config._dayOfYear = toInt(input);
1129 datePartArray[YEAR] = moment.parseTwoDigitYear(input);
1134 datePartArray[YEAR] = toInt(input);
1137 case 'a' : // fall through to A
1139 config._isPm = getLangDefinition(config._l).isPM(input);
1142 case 'H' : // fall through to hh
1143 case 'HH' : // fall through to hh
1144 case 'h' : // fall through to hh
1146 datePartArray[HOUR] = toInt(input);
1149 case 'm' : // fall through to mm
1151 datePartArray[MINUTE] = toInt(input);
1154 case 's' : // fall through to ss
1156 datePartArray[SECOND] = toInt(input);
1163 datePartArray[MILLISECOND] = toInt(('0.' + input) * 1000);
1165 // UNIX TIMESTAMP WITH MS
1167 config._d = new Date(parseFloat(input) * 1000);
1170 case 'Z' : // fall through to ZZ
1172 config._useUTC = true;
1173 config._tzm = timezoneMinutesFromString(input);
1179 a = getLangDefinition(config._l).weekdaysParse(input);
1180 // if we didn't get a weekday name, mark the date as invalid
1182 config._w = config._w || {};
1185 config._pf.invalidWeekday = input;
1188 // WEEK, WEEK DAY - numeric
1196 token = token.substr(0, 1);
1201 token = token.substr(0, 2);
1203 config._w = config._w || {};
1204 config._w[token] = toInt(input);
1209 config._w = config._w || {};
1210 config._w[token] = moment.parseTwoDigitYear(input);
1214 function dayOfYearFromWeekInfo(config) {
1215 var w, weekYear, week, weekday, dow, doy, temp, lang;
1218 if (w.GG != null || w.W != null || w.E != null) {
1222 // TODO: We need to take the current isoWeekYear, but that depends on
1223 // how we interpret now (local, utc, fixed offset). So create
1224 // a now version of current config (take local/utc/offset flags, and
1226 weekYear = dfl(w.GG, config._a[YEAR], weekOfYear(moment(), 1, 4).year);
1228 weekday = dfl(w.E, 1);
1230 lang = getLangDefinition(config._l);
1231 dow = lang._week.dow;
1232 doy = lang._week.doy;
1234 weekYear = dfl(w.gg, config._a[YEAR], weekOfYear(moment(), dow, doy).year);
1238 // weekday -- low day numbers are considered next week
1240 if (weekday < dow) {
1243 } else if (w.e != null) {
1244 // local weekday -- counting starts from begining of week
1245 weekday = w.e + dow;
1247 // default to begining of week
1251 temp = dayOfYearFromWeeks(weekYear, week, weekday, doy, dow);
1253 config._a[YEAR] = temp.year;
1254 config._dayOfYear = temp.dayOfYear;
1257 // convert an array to a date.
1258 // the array should mirror the parameters below
1259 // note: all values past the year are optional and will default to the lowest possible value.
1260 // [year, month, day , hour, minute, second, millisecond]
1261 function dateFromConfig(config) {
1262 var i, date, input = [], currentDate, yearToUse;
1268 currentDate = currentDateArray(config);
1270 //compute day of the year from weeks and weekdays
1271 if (config._w && config._a[DATE] == null && config._a[MONTH] == null) {
1272 dayOfYearFromWeekInfo(config);
1275 //if the day of the year is set, figure out what it is
1276 if (config._dayOfYear) {
1277 yearToUse = dfl(config._a[YEAR], currentDate[YEAR]);
1279 if (config._dayOfYear > daysInYear(yearToUse)) {
1280 config._pf._overflowDayOfYear = true;
1283 date = makeUTCDate(yearToUse, 0, config._dayOfYear);
1284 config._a[MONTH] = date.getUTCMonth();
1285 config._a[DATE] = date.getUTCDate();
1288 // Default to current date.
1289 // * if no year, month, day of month are given, default to today
1290 // * if day of month is given, default month and year
1291 // * if month is given, default only year
1292 // * if year is given, don't default anything
1293 for (i = 0; i < 3 && config._a[i] == null; ++i) {
1294 config._a[i] = input[i] = currentDate[i];
1297 // Zero out whatever was not defaulted, including time
1298 for (; i < 7; i++) {
1299 config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
1302 config._d = (config._useUTC ? makeUTCDate : makeDate).apply(null, input);
1303 // Apply timezone offset from input. The actual zone can be changed
1305 if (config._tzm != null) {
1306 config._d.setUTCMinutes(config._d.getUTCMinutes() + config._tzm);
1310 function dateFromObject(config) {
1311 var normalizedInput;
1317 normalizedInput = normalizeObjectUnits(config._i);
1319 normalizedInput.year,
1320 normalizedInput.month,
1321 normalizedInput.day,
1322 normalizedInput.hour,
1323 normalizedInput.minute,
1324 normalizedInput.second,
1325 normalizedInput.millisecond
1328 dateFromConfig(config);
1331 function currentDateArray(config) {
1332 var now = new Date();
1333 if (config._useUTC) {
1335 now.getUTCFullYear(),
1340 return [now.getFullYear(), now.getMonth(), now.getDate()];
1344 // date from string and format string
1345 function makeDateFromStringAndFormat(config) {
1347 if (config._f === moment.ISO_8601) {
1353 config._pf.empty = true;
1355 // This array is used to make a Date, either with `new Date` or `Date.UTC`
1356 var lang = getLangDefinition(config._l),
1357 string = '' + config._i,
1358 i, parsedInput, tokens, token, skipped,
1359 stringLength = string.length,
1360 totalParsedInputLength = 0;
1362 tokens = expandFormat(config._f, lang).match(formattingTokens) || [];
1364 for (i = 0; i < tokens.length; i++) {
1366 parsedInput = (string.match(getParseRegexForToken(token, config)) || [])[0];
1368 skipped = string.substr(0, string.indexOf(parsedInput));
1369 if (skipped.length > 0) {
1370 config._pf.unusedInput.push(skipped);
1372 string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
1373 totalParsedInputLength += parsedInput.length;
1375 // don't parse if it's not a known token
1376 if (formatTokenFunctions[token]) {
1378 config._pf.empty = false;
1381 config._pf.unusedTokens.push(token);
1383 addTimeToArrayFromToken(token, parsedInput, config);
1385 else if (config._strict && !parsedInput) {
1386 config._pf.unusedTokens.push(token);
1390 // add remaining unparsed input length to the string
1391 config._pf.charsLeftOver = stringLength - totalParsedInputLength;
1392 if (string.length > 0) {
1393 config._pf.unusedInput.push(string);
1397 if (config._isPm && config._a[HOUR] < 12) {
1398 config._a[HOUR] += 12;
1400 // if is 12 am, change hours to 0
1401 if (config._isPm === false && config._a[HOUR] === 12) {
1402 config._a[HOUR] = 0;
1405 dateFromConfig(config);
1406 checkOverflow(config);
1409 function unescapeFormat(s) {
1410 return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) {
1411 return p1 || p2 || p3 || p4;
1415 // Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
1416 function regexpEscape(s) {
1417 return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
1420 // date from string and array of format strings
1421 function makeDateFromStringAndArray(config) {
1429 if (config._f.length === 0) {
1430 config._pf.invalidFormat = true;
1431 config._d = new Date(NaN);
1435 for (i = 0; i < config._f.length; i++) {
1437 tempConfig = extend({}, config);
1438 tempConfig._pf = defaultParsingFlags();
1439 tempConfig._f = config._f[i];
1440 makeDateFromStringAndFormat(tempConfig);
1442 if (!isValid(tempConfig)) {
1446 // if there is any input that was not parsed add a penalty for that format
1447 currentScore += tempConfig._pf.charsLeftOver;
1450 currentScore += tempConfig._pf.unusedTokens.length * 10;
1452 tempConfig._pf.score = currentScore;
1454 if (scoreToBeat == null || currentScore < scoreToBeat) {
1455 scoreToBeat = currentScore;
1456 bestMoment = tempConfig;
1460 extend(config, bestMoment || tempConfig);
1463 // date from iso format
1464 function parseISO(config) {
1467 match = isoRegex.exec(string);
1470 config._pf.iso = true;
1471 for (i = 0, l = isoDates.length; i < l; i++) {
1472 if (isoDates[i][1].exec(string)) {
1473 // match[5] should be "T" or undefined
1474 config._f = isoDates[i][0] + (match[6] || " ");
1478 for (i = 0, l = isoTimes.length; i < l; i++) {
1479 if (isoTimes[i][1].exec(string)) {
1480 config._f += isoTimes[i][0];
1484 if (string.match(parseTokenTimezone)) {
1487 makeDateFromStringAndFormat(config);
1489 config._isValid = false;
1493 // date from iso format or fallback
1494 function makeDateFromString(config) {
1496 if (config._isValid === false) {
1497 delete config._isValid;
1498 moment.createFromInputFallback(config);
1502 function makeDateFromInput(config) {
1503 var input = config._i,
1504 matched = aspNetJsonRegex.exec(input);
1506 if (input === undefined) {
1507 config._d = new Date();
1508 } else if (matched) {
1509 config._d = new Date(+matched[1]);
1510 } else if (typeof input === 'string') {
1511 makeDateFromString(config);
1512 } else if (isArray(input)) {
1513 config._a = input.slice(0);
1514 dateFromConfig(config);
1515 } else if (isDate(input)) {
1516 config._d = new Date(+input);
1517 } else if (typeof(input) === 'object') {
1518 dateFromObject(config);
1519 } else if (typeof(input) === 'number') {
1520 // from milliseconds
1521 config._d = new Date(input);
1523 moment.createFromInputFallback(config);
1527 function makeDate(y, m, d, h, M, s, ms) {
1528 //can't just apply() to create a date:
1529 //http://stackoverflow.com/questions/181348/instantiating-a-javascript-object-by-calling-prototype-constructor-apply
1530 var date = new Date(y, m, d, h, M, s, ms);
1532 //the date constructor doesn't accept years < 1970
1534 date.setFullYear(y);
1539 function makeUTCDate(y) {
1540 var date = new Date(Date.UTC.apply(null, arguments));
1542 date.setUTCFullYear(y);
1547 function parseWeekday(input, language) {
1548 if (typeof input === 'string') {
1549 if (!isNaN(input)) {
1550 input = parseInt(input, 10);
1553 input = language.weekdaysParse(input);
1554 if (typeof input !== 'number') {
1562 /************************************
1564 ************************************/
1567 // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
1568 function substituteTimeAgo(string, number, withoutSuffix, isFuture, lang) {
1569 return lang.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
1572 function relativeTime(milliseconds, withoutSuffix, lang) {
1573 var seconds = round(Math.abs(milliseconds) / 1000),
1574 minutes = round(seconds / 60),
1575 hours = round(minutes / 60),
1576 days = round(hours / 24),
1577 years = round(days / 365),
1578 args = seconds < relativeTimeThresholds.s && ['s', seconds] ||
1579 minutes === 1 && ['m'] ||
1580 minutes < relativeTimeThresholds.m && ['mm', minutes] ||
1581 hours === 1 && ['h'] ||
1582 hours < relativeTimeThresholds.h && ['hh', hours] ||
1583 days === 1 && ['d'] ||
1584 days <= relativeTimeThresholds.dd && ['dd', days] ||
1585 days <= relativeTimeThresholds.dm && ['M'] ||
1586 days < relativeTimeThresholds.dy && ['MM', round(days / 30)] ||
1587 years === 1 && ['y'] || ['yy', years];
1588 args[2] = withoutSuffix;
1589 args[3] = milliseconds > 0;
1591 return substituteTimeAgo.apply({}, args);
1595 /************************************
1597 ************************************/
1600 // firstDayOfWeek 0 = sun, 6 = sat
1601 // the day of the week that starts the week
1602 // (usually sunday or monday)
1603 // firstDayOfWeekOfYear 0 = sun, 6 = sat
1604 // the first week is the week that contains the first
1605 // of this day of the week
1606 // (eg. ISO weeks use thursday (4))
1607 function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
1608 var end = firstDayOfWeekOfYear - firstDayOfWeek,
1609 daysToDayOfWeek = firstDayOfWeekOfYear - mom.day(),
1613 if (daysToDayOfWeek > end) {
1614 daysToDayOfWeek -= 7;
1617 if (daysToDayOfWeek < end - 7) {
1618 daysToDayOfWeek += 7;
1621 adjustedMoment = moment(mom).add('d', daysToDayOfWeek);
1623 week: Math.ceil(adjustedMoment.dayOfYear() / 7),
1624 year: adjustedMoment.year()
1628 //http://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday
1629 function dayOfYearFromWeeks(year, week, weekday, firstDayOfWeekOfYear, firstDayOfWeek) {
1630 var d = makeUTCDate(year, 0, 1).getUTCDay(), daysToAdd, dayOfYear;
1632 d = d === 0 ? 7 : d;
1633 weekday = weekday != null ? weekday : firstDayOfWeek;
1634 daysToAdd = firstDayOfWeek - d + (d > firstDayOfWeekOfYear ? 7 : 0) - (d < firstDayOfWeek ? 7 : 0);
1635 dayOfYear = 7 * (week - 1) + (weekday - firstDayOfWeek) + daysToAdd + 1;
1638 year: dayOfYear > 0 ? year : year - 1,
1639 dayOfYear: dayOfYear > 0 ? dayOfYear : daysInYear(year - 1) + dayOfYear
1643 /************************************
1645 ************************************/
1647 function makeMoment(config) {
1648 var input = config._i,
1651 if (input === null || (format === undefined && input === '')) {
1652 return moment.invalid({nullInput: true});
1655 if (typeof input === 'string') {
1656 config._i = input = getLangDefinition().preparse(input);
1659 if (moment.isMoment(input)) {
1660 config = cloneMoment(input);
1662 config._d = new Date(+input._d);
1663 } else if (format) {
1664 if (isArray(format)) {
1665 makeDateFromStringAndArray(config);
1667 makeDateFromStringAndFormat(config);
1670 makeDateFromInput(config);
1673 return new Moment(config);
1676 moment = function (input, format, lang, strict) {
1679 if (typeof(lang) === "boolean") {
1683 // object construction must be done this way.
1684 // https://github.com/moment/moment/issues/1423
1686 c._isAMomentObject = true;
1692 c._pf = defaultParsingFlags();
1694 return makeMoment(c);
1697 moment.suppressDeprecationWarnings = false;
1699 moment.createFromInputFallback = deprecate(
1700 "moment construction falls back to js Date. This is " +
1701 "discouraged and will be removed in upcoming major " +
1702 "release. Please refer to " +
1703 "https://github.com/moment/moment/issues/1407 for more info.",
1705 config._d = new Date(config._i);
1708 // Pick a moment m from moments so that m[fn](other) is true for all
1709 // other. This relies on the function fn to be transitive.
1711 // moments should either be an array of moment objects or an array, whose
1712 // first element is an array of moment objects.
1713 function pickBy(fn, moments) {
1715 if (moments.length === 1 && isArray(moments[0])) {
1716 moments = moments[0];
1718 if (!moments.length) {
1722 for (i = 1; i < moments.length; ++i) {
1723 if (moments[i][fn](res)) {
1730 moment.min = function () {
1731 var args = [].slice.call(arguments, 0);
1733 return pickBy('isBefore', args);
1736 moment.max = function () {
1737 var args = [].slice.call(arguments, 0);
1739 return pickBy('isAfter', args);
1742 // creating with utc
1743 moment.utc = function (input, format, lang, strict) {
1746 if (typeof(lang) === "boolean") {
1750 // object construction must be done this way.
1751 // https://github.com/moment/moment/issues/1423
1753 c._isAMomentObject = true;
1760 c._pf = defaultParsingFlags();
1762 return makeMoment(c).utc();
1765 // creating with unix timestamp (in seconds)
1766 moment.unix = function (input) {
1767 return moment(input * 1000);
1771 moment.duration = function (input, key) {
1772 var duration = input,
1773 // matching against regexp is expensive, do it on demand
1779 if (moment.isDuration(input)) {
1781 ms: input._milliseconds,
1785 } else if (typeof input === 'number') {
1788 duration[key] = input;
1790 duration.milliseconds = input;
1792 } else if (!!(match = aspNetTimeSpanJsonRegex.exec(input))) {
1793 sign = (match[1] === "-") ? -1 : 1;
1796 d: toInt(match[DATE]) * sign,
1797 h: toInt(match[HOUR]) * sign,
1798 m: toInt(match[MINUTE]) * sign,
1799 s: toInt(match[SECOND]) * sign,
1800 ms: toInt(match[MILLISECOND]) * sign
1802 } else if (!!(match = isoDurationRegex.exec(input))) {
1803 sign = (match[1] === "-") ? -1 : 1;
1804 parseIso = function (inp) {
1805 // We'd normally use ~~inp for this, but unfortunately it also
1806 // converts floats to ints.
1807 // inp may be undefined, so careful calling replace on it.
1808 var res = inp && parseFloat(inp.replace(',', '.'));
1809 // apply sign while we're at it
1810 return (isNaN(res) ? 0 : res) * sign;
1813 y: parseIso(match[2]),
1814 M: parseIso(match[3]),
1815 d: parseIso(match[4]),
1816 h: parseIso(match[5]),
1817 m: parseIso(match[6]),
1818 s: parseIso(match[7]),
1819 w: parseIso(match[8])
1823 ret = new Duration(duration);
1825 if (moment.isDuration(input) && input.hasOwnProperty('_lang')) {
1826 ret._lang = input._lang;
1833 moment.version = VERSION;
1836 moment.defaultFormat = isoFormat;
1838 // constant that refers to the ISO standard
1839 moment.ISO_8601 = function () {};
1841 // Plugins that add properties should also add the key here (null value),
1842 // so we can properly clone ourselves.
1843 moment.momentProperties = momentProperties;
1845 // This function will be called whenever a moment is mutated.
1846 // It is intended to keep the offset in sync with the timezone.
1847 moment.updateOffset = function () {};
1849 // This function allows you to set a threshold for relative time strings
1850 moment.relativeTimeThreshold = function(threshold, limit) {
1851 if (relativeTimeThresholds[threshold] === undefined) {
1854 relativeTimeThresholds[threshold] = limit;
1858 // This function will load languages and then set the global language. If
1859 // no arguments are passed in, it will simply return the current global
1861 moment.lang = function (key, values) {
1864 return moment.fn._lang._abbr;
1867 loadLang(normalizeLanguage(key), values);
1868 } else if (values === null) {
1871 } else if (!languages[key]) {
1872 getLangDefinition(key);
1874 r = moment.duration.fn._lang = moment.fn._lang = getLangDefinition(key);
1878 // returns language data
1879 moment.langData = function (key) {
1880 if (key && key._lang && key._lang._abbr) {
1881 key = key._lang._abbr;
1883 return getLangDefinition(key);
1886 // compare moment object
1887 moment.isMoment = function (obj) {
1888 return obj instanceof Moment ||
1889 (obj != null && obj.hasOwnProperty('_isAMomentObject'));
1892 // for typechecking Duration objects
1893 moment.isDuration = function (obj) {
1894 return obj instanceof Duration;
1897 for (i = lists.length - 1; i >= 0; --i) {
1901 moment.normalizeUnits = function (units) {
1902 return normalizeUnits(units);
1905 moment.invalid = function (flags) {
1906 var m = moment.utc(NaN);
1907 if (flags != null) {
1908 extend(m._pf, flags);
1911 m._pf.userInvalidated = true;
1917 moment.parseZone = function () {
1918 return moment.apply(null, arguments).parseZone();
1921 moment.parseTwoDigitYear = function (input) {
1922 return toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
1925 /************************************
1927 ************************************/
1930 extend(moment.fn = Moment.prototype, {
1932 clone : function () {
1933 return moment(this);
1936 valueOf : function () {
1937 return +this._d + ((this._offset || 0) * 60000);
1940 unix : function () {
1941 return Math.floor(+this / 1000);
1944 toString : function () {
1945 return this.clone().lang('en').format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ");
1948 toDate : function () {
1949 return this._offset ? new Date(+this) : this._d;
1952 toISOString : function () {
1953 var m = moment(this).utc();
1954 if (0 < m.year() && m.year() <= 9999) {
1955 return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
1957 return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
1961 toArray : function () {
1974 isValid : function () {
1975 return isValid(this);
1978 isDSTShifted : function () {
1981 return this.isValid() && compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray()) > 0;
1987 parsingFlags : function () {
1988 return extend({}, this._pf);
1991 invalidAt: function () {
1992 return this._pf.overflow;
1996 return this.zone(0);
1999 local : function () {
2001 this._isUTC = false;
2005 format : function (inputString) {
2006 var output = formatMoment(this, inputString || moment.defaultFormat);
2007 return this.lang().postformat(output);
2010 add : function (input, val) {
2012 // switch args to support add('s', 1) and add(1, 's')
2013 if (typeof input === 'string' && typeof val === 'string') {
2014 dur = moment.duration(isNaN(+val) ? +input : +val, isNaN(+val) ? val : input);
2015 } else if (typeof input === 'string') {
2016 dur = moment.duration(+val, input);
2018 dur = moment.duration(input, val);
2020 addOrSubtractDurationFromMoment(this, dur, 1);
2024 subtract : function (input, val) {
2026 // switch args to support subtract('s', 1) and subtract(1, 's')
2027 if (typeof input === 'string' && typeof val === 'string') {
2028 dur = moment.duration(isNaN(+val) ? +input : +val, isNaN(+val) ? val : input);
2029 } else if (typeof input === 'string') {
2030 dur = moment.duration(+val, input);
2032 dur = moment.duration(input, val);
2034 addOrSubtractDurationFromMoment(this, dur, -1);
2038 diff : function (input, units, asFloat) {
2039 var that = makeAs(input, this),
2040 zoneDiff = (this.zone() - that.zone()) * 6e4,
2043 units = normalizeUnits(units);
2045 if (units === 'year' || units === 'month') {
2046 // average number of days in the months in the given dates
2047 diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
2048 // difference in months
2049 output = ((this.year() - that.year()) * 12) + (this.month() - that.month());
2050 // adjust by taking difference in days, average number of days
2051 // and dst in the given months.
2052 output += ((this - moment(this).startOf('month')) -
2053 (that - moment(that).startOf('month'))) / diff;
2054 // same as above but with zones, to negate all dst
2055 output -= ((this.zone() - moment(this).startOf('month').zone()) -
2056 (that.zone() - moment(that).startOf('month').zone())) * 6e4 / diff;
2057 if (units === 'year') {
2058 output = output / 12;
2061 diff = (this - that);
2062 output = units === 'second' ? diff / 1e3 : // 1000
2063 units === 'minute' ? diff / 6e4 : // 1000 * 60
2064 units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
2065 units === 'day' ? (diff - zoneDiff) / 864e5 : // 1000 * 60 * 60 * 24, negate dst
2066 units === 'week' ? (diff - zoneDiff) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst
2069 return asFloat ? output : absRound(output);
2072 from : function (time, withoutSuffix) {
2073 return moment.duration(this.diff(time)).lang(this.lang()._abbr).humanize(!withoutSuffix);
2076 fromNow : function (withoutSuffix) {
2077 return this.from(moment(), withoutSuffix);
2080 calendar : function (time) {
2081 // We want to compare the start of today, vs this.
2082 // Getting start-of-today depends on whether we're zone'd or not.
2083 var now = time || moment(),
2084 sod = makeAs(now, this).startOf('day'),
2085 diff = this.diff(sod, 'days', true),
2086 format = diff < -6 ? 'sameElse' :
2087 diff < -1 ? 'lastWeek' :
2088 diff < 0 ? 'lastDay' :
2089 diff < 1 ? 'sameDay' :
2090 diff < 2 ? 'nextDay' :
2091 diff < 7 ? 'nextWeek' : 'sameElse';
2092 return this.format(this.lang().calendar(format, this));
2095 isLeapYear : function () {
2096 return isLeapYear(this.year());
2099 isDST : function () {
2100 return (this.zone() < this.clone().month(0).zone() ||
2101 this.zone() < this.clone().month(5).zone());
2104 day : function (input) {
2105 var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
2106 if (input != null) {
2107 input = parseWeekday(input, this.lang());
2108 return this.add({ d : input - day });
2114 month : makeAccessor('Month', true),
2116 startOf: function (units) {
2117 units = normalizeUnits(units);
2118 // the following switch intentionally omits break keywords
2119 // to utilize falling through the cases.
2140 this.milliseconds(0);
2144 // weeks are a special case
2145 if (units === 'week') {
2147 } else if (units === 'isoWeek') {
2151 // quarters are also special
2152 if (units === 'quarter') {
2153 this.month(Math.floor(this.month() / 3) * 3);
2159 endOf: function (units) {
2160 units = normalizeUnits(units);
2161 return this.startOf(units).add((units === 'isoWeek' ? 'week' : units), 1).subtract('ms', 1);
2164 isAfter: function (input, units) {
2165 units = typeof units !== 'undefined' ? units : 'millisecond';
2166 return +this.clone().startOf(units) > +moment(input).startOf(units);
2169 isBefore: function (input, units) {
2170 units = typeof units !== 'undefined' ? units : 'millisecond';
2171 return +this.clone().startOf(units) < +moment(input).startOf(units);
2174 isSame: function (input, units) {
2175 units = units || 'ms';
2176 return +this.clone().startOf(units) === +makeAs(input, this).startOf(units);
2180 "moment().min is deprecated, use moment.min instead. https://github.com/moment/moment/issues/1548",
2182 other = moment.apply(null, arguments);
2183 return other < this ? this : other;
2188 "moment().max is deprecated, use moment.max instead. https://github.com/moment/moment/issues/1548",
2190 other = moment.apply(null, arguments);
2191 return other > this ? this : other;
2195 // keepTime = true means only change the timezone, without affecting
2196 // the local hour. So 5:31:26 +0300 --[zone(2, true)]--> 5:31:26 +0200
2197 // It is possible that 5:31:26 doesn't exist int zone +0200, so we
2198 // adjust the time as needed, to be valid.
2200 // Keeping the time actually adds/subtracts (one hour)
2201 // from the actual represented time. That is why we call updateOffset
2202 // a second time. In case it wants us to change the offset again
2203 // _changeInProgress == true case, then we have to adjust, because
2204 // there is no such time in the given timezone.
2205 zone : function (input, keepTime) {
2206 var offset = this._offset || 0;
2207 if (input != null) {
2208 if (typeof input === "string") {
2209 input = timezoneMinutesFromString(input);
2211 if (Math.abs(input) < 16) {
2214 this._offset = input;
2216 if (offset !== input) {
2217 if (!keepTime || this._changeInProgress) {
2218 addOrSubtractDurationFromMoment(this,
2219 moment.duration(offset - input, 'm'), 1, false);
2220 } else if (!this._changeInProgress) {
2221 this._changeInProgress = true;
2222 moment.updateOffset(this, true);
2223 this._changeInProgress = null;
2227 return this._isUTC ? offset : this._d.getTimezoneOffset();
2232 zoneAbbr : function () {
2233 return this._isUTC ? "UTC" : "";
2236 zoneName : function () {
2237 return this._isUTC ? "Coordinated Universal Time" : "";
2240 parseZone : function () {
2242 this.zone(this._tzm);
2243 } else if (typeof this._i === 'string') {
2249 hasAlignedHourOffset : function (input) {
2254 input = moment(input).zone();
2257 return (this.zone() - input) % 60 === 0;
2260 daysInMonth : function () {
2261 return daysInMonth(this.year(), this.month());
2264 dayOfYear : function (input) {
2265 var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
2266 return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
2269 quarter : function (input) {
2270 return input == null ? Math.ceil((this.month() + 1) / 3) : this.month((input - 1) * 3 + this.month() % 3);
2273 weekYear : function (input) {
2274 var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year;
2275 return input == null ? year : this.add("y", (input - year));
2278 isoWeekYear : function (input) {
2279 var year = weekOfYear(this, 1, 4).year;
2280 return input == null ? year : this.add("y", (input - year));
2283 week : function (input) {
2284 var week = this.lang().week(this);
2285 return input == null ? week : this.add("d", (input - week) * 7);
2288 isoWeek : function (input) {
2289 var week = weekOfYear(this, 1, 4).week;
2290 return input == null ? week : this.add("d", (input - week) * 7);
2293 weekday : function (input) {
2294 var weekday = (this.day() + 7 - this.lang()._week.dow) % 7;
2295 return input == null ? weekday : this.add("d", input - weekday);
2298 isoWeekday : function (input) {
2299 // behaves the same as moment#day except
2300 // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
2301 // as a setter, sunday should belong to the previous week.
2302 return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);
2305 isoWeeksInYear : function () {
2306 return weeksInYear(this.year(), 1, 4);
2309 weeksInYear : function () {
2310 var weekInfo = this._lang._week;
2311 return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy);
2314 get : function (units) {
2315 units = normalizeUnits(units);
2316 return this[units]();
2319 set : function (units, value) {
2320 units = normalizeUnits(units);
2321 if (typeof this[units] === 'function') {
2327 // If passed a language key, it will set the language for this
2328 // instance. Otherwise, it will return the language configuration
2329 // variables for this instance.
2330 lang : function (key) {
2331 if (key === undefined) {
2334 this._lang = getLangDefinition(key);
2340 function rawMonthSetter(mom, value) {
2343 // TODO: Move this out of here!
2344 if (typeof value === 'string') {
2345 value = mom.lang().monthsParse(value);
2346 // TODO: Another silent failure?
2347 if (typeof value !== 'number') {
2352 dayOfMonth = Math.min(mom.date(),
2353 daysInMonth(mom.year(), value));
2354 mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth);
2358 function rawGetter(mom, unit) {
2359 return mom._d['get' + (mom._isUTC ? 'UTC' : '') + unit]();
2362 function rawSetter(mom, unit, value) {
2363 if (unit === 'Month') {
2364 return rawMonthSetter(mom, value);
2366 return mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
2370 function makeAccessor(unit, keepTime) {
2371 return function (value) {
2372 if (value != null) {
2373 rawSetter(this, unit, value);
2374 moment.updateOffset(this, keepTime);
2377 return rawGetter(this, unit);
2382 moment.fn.millisecond = moment.fn.milliseconds = makeAccessor('Milliseconds', false);
2383 moment.fn.second = moment.fn.seconds = makeAccessor('Seconds', false);
2384 moment.fn.minute = moment.fn.minutes = makeAccessor('Minutes', false);
2385 // Setting the hour should keep the time, because the user explicitly
2386 // specified which hour he wants. So trying to maintain the same hour (in
2387 // a new timezone) makes sense. Adding/subtracting hours does not follow
2389 moment.fn.hour = moment.fn.hours = makeAccessor('Hours', true);
2390 // moment.fn.month is defined separately
2391 moment.fn.date = makeAccessor('Date', true);
2392 moment.fn.dates = deprecate("dates accessor is deprecated. Use date instead.", makeAccessor('Date', true));
2393 moment.fn.year = makeAccessor('FullYear', true);
2394 moment.fn.years = deprecate("years accessor is deprecated. Use year instead.", makeAccessor('FullYear', true));
2396 // add plural methods
2397 moment.fn.days = moment.fn.day;
2398 moment.fn.months = moment.fn.month;
2399 moment.fn.weeks = moment.fn.week;
2400 moment.fn.isoWeeks = moment.fn.isoWeek;
2401 moment.fn.quarters = moment.fn.quarter;
2403 // add aliased format methods
2404 moment.fn.toJSON = moment.fn.toISOString;
2406 /************************************
2408 ************************************/
2411 extend(moment.duration.fn = Duration.prototype, {
2413 _bubble : function () {
2414 var milliseconds = this._milliseconds,
2416 months = this._months,
2418 seconds, minutes, hours, years;
2420 // The following code bubbles up values, see the tests for
2421 // examples of what that means.
2422 data.milliseconds = milliseconds % 1000;
2424 seconds = absRound(milliseconds / 1000);
2425 data.seconds = seconds % 60;
2427 minutes = absRound(seconds / 60);
2428 data.minutes = minutes % 60;
2430 hours = absRound(minutes / 60);
2431 data.hours = hours % 24;
2433 days += absRound(hours / 24);
2434 data.days = days % 30;
2436 months += absRound(days / 30);
2437 data.months = months % 12;
2439 years = absRound(months / 12);
2443 weeks : function () {
2444 return absRound(this.days() / 7);
2447 valueOf : function () {
2448 return this._milliseconds +
2449 this._days * 864e5 +
2450 (this._months % 12) * 2592e6 +
2451 toInt(this._months / 12) * 31536e6;
2454 humanize : function (withSuffix) {
2455 var difference = +this,
2456 output = relativeTime(difference, !withSuffix, this.lang());
2459 output = this.lang().pastFuture(difference, output);
2462 return this.lang().postformat(output);
2465 add : function (input, val) {
2466 // supports only 2.0-style add(1, 's') or add(moment)
2467 var dur = moment.duration(input, val);
2469 this._milliseconds += dur._milliseconds;
2470 this._days += dur._days;
2471 this._months += dur._months;
2478 subtract : function (input, val) {
2479 var dur = moment.duration(input, val);
2481 this._milliseconds -= dur._milliseconds;
2482 this._days -= dur._days;
2483 this._months -= dur._months;
2490 get : function (units) {
2491 units = normalizeUnits(units);
2492 return this[units.toLowerCase() + 's']();
2495 as : function (units) {
2496 units = normalizeUnits(units);
2497 return this['as' + units.charAt(0).toUpperCase() + units.slice(1) + 's']();
2500 lang : moment.fn.lang,
2502 toIsoString : function () {
2503 // inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js
2504 var years = Math.abs(this.years()),
2505 months = Math.abs(this.months()),
2506 days = Math.abs(this.days()),
2507 hours = Math.abs(this.hours()),
2508 minutes = Math.abs(this.minutes()),
2509 seconds = Math.abs(this.seconds() + this.milliseconds() / 1000);
2511 if (!this.asSeconds()) {
2512 // this is the same as C#'s (Noda) and python (isodate)...
2513 // but not other JS (goog.date)
2517 return (this.asSeconds() < 0 ? '-' : '') +
2519 (years ? years + 'Y' : '') +
2520 (months ? months + 'M' : '') +
2521 (days ? days + 'D' : '') +
2522 ((hours || minutes || seconds) ? 'T' : '') +
2523 (hours ? hours + 'H' : '') +
2524 (minutes ? minutes + 'M' : '') +
2525 (seconds ? seconds + 'S' : '');
2529 function makeDurationGetter(name) {
2530 moment.duration.fn[name] = function () {
2531 return this._data[name];
2535 function makeDurationAsGetter(name, factor) {
2536 moment.duration.fn['as' + name] = function () {
2537 return +this / factor;
2541 for (i in unitMillisecondFactors) {
2542 if (unitMillisecondFactors.hasOwnProperty(i)) {
2543 makeDurationAsGetter(i, unitMillisecondFactors[i]);
2544 makeDurationGetter(i.toLowerCase());
2548 makeDurationAsGetter('Weeks', 6048e5);
2549 moment.duration.fn.asMonths = function () {
2550 return (+this - this.years() * 31536e6) / 2592e6 + this.years() * 12;
2554 /************************************
2556 ************************************/
2559 // Set default language, other languages will inherit from English.
2561 ordinal : function (number) {
2562 var b = number % 10,
2563 output = (toInt(number % 100 / 10) === 1) ? 'th' :
2566 (b === 3) ? 'rd' : 'th';
2567 return number + output;
2571 /* EMBED_LANGUAGES */
2573 /************************************
2575 ************************************/
2577 function makeGlobal(shouldDeprecate) {
2578 /*global ender:false */
2579 if (typeof ender !== 'undefined') {
2582 oldGlobalMoment = globalScope.moment;
2583 if (shouldDeprecate) {
2584 globalScope.moment = deprecate(
2585 "Accessing Moment through the global scope is " +
2586 "deprecated, and will be removed in an upcoming " +
2590 globalScope.moment = moment;
2594 // CommonJS module is defined
2596 module.exports = moment;
2597 } else if (typeof define === "function" && define.amd) {
2598 define("moment", function (require, exports, module) {
2599 if (module.config && module.config() && module.config().noGlobal === true) {
2600 // release the global variable
2601 globalScope.moment = oldGlobalMoment;