moment.js 83 KB

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