430dbbf503284aa45e281b0009404d2d7c636043c5c2767a8e9d4752d00311bdb95ffaa2bfa500aa4f338f398cdae0ed26f8461f0f26953065f1549a4843b6 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Modified from https://github.com/taylorhakes/fecha
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2015 Taylor Hakes
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /*eslint-disable*/
  26. // 把 YYYY-MM-DD 改成了 yyyy-MM-dd
  27. (function (main) {
  28. 'use strict';
  29. /**
  30. * Parse or format dates
  31. * @class fecha
  32. */
  33. var fecha = {};
  34. var token = /d{1,4}|M{1,4}|yy(?:yy)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
  35. var twoDigits = '\\d\\d?';
  36. var threeDigits = '\\d{3}';
  37. var fourDigits = '\\d{4}';
  38. var word = '[^\\s]+';
  39. var literal = /\[([^]*?)\]/gm;
  40. var noop = function () {
  41. };
  42. function regexEscape(str) {
  43. return str.replace( /[|\\{()[^$+*?.-]/g, '\\$&');
  44. }
  45. function shorten(arr, sLen) {
  46. var newArr = [];
  47. for (var i = 0, len = arr.length; i < len; i++) {
  48. newArr.push(arr[i].substr(0, sLen));
  49. }
  50. return newArr;
  51. }
  52. function monthUpdate(arrName) {
  53. return function (d, v, i18n) {
  54. var index = i18n[arrName].indexOf(v.charAt(0).toUpperCase() + v.substr(1).toLowerCase());
  55. if (~index) {
  56. d.month = index;
  57. }
  58. };
  59. }
  60. function pad(val, len) {
  61. val = String(val);
  62. len = len || 2;
  63. while (val.length < len) {
  64. val = '0' + val;
  65. }
  66. return val;
  67. }
  68. var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  69. var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  70. var monthNamesShort = shorten(monthNames, 3);
  71. var dayNamesShort = shorten(dayNames, 3);
  72. fecha.i18n = {
  73. dayNamesShort: dayNamesShort,
  74. dayNames: dayNames,
  75. monthNamesShort: monthNamesShort,
  76. monthNames: monthNames,
  77. amPm: ['am', 'pm'],
  78. DoFn: function DoFn(D) {
  79. return D + ['th', 'st', 'nd', 'rd'][D % 10 > 3 ? 0 : (D - D % 10 !== 10) * D % 10];
  80. }
  81. };
  82. var formatFlags = {
  83. D: function(dateObj) {
  84. return dateObj.getDay();
  85. },
  86. DD: function(dateObj) {
  87. return pad(dateObj.getDay());
  88. },
  89. Do: function(dateObj, i18n) {
  90. return i18n.DoFn(dateObj.getDate());
  91. },
  92. d: function(dateObj) {
  93. return dateObj.getDate();
  94. },
  95. dd: function(dateObj) {
  96. return pad(dateObj.getDate());
  97. },
  98. ddd: function(dateObj, i18n) {
  99. return i18n.dayNamesShort[dateObj.getDay()];
  100. },
  101. dddd: function(dateObj, i18n) {
  102. return i18n.dayNames[dateObj.getDay()];
  103. },
  104. M: function(dateObj) {
  105. return dateObj.getMonth() + 1;
  106. },
  107. MM: function(dateObj) {
  108. return pad(dateObj.getMonth() + 1);
  109. },
  110. MMM: function(dateObj, i18n) {
  111. return i18n.monthNamesShort[dateObj.getMonth()];
  112. },
  113. MMMM: function(dateObj, i18n) {
  114. return i18n.monthNames[dateObj.getMonth()];
  115. },
  116. yy: function(dateObj) {
  117. return pad(String(dateObj.getFullYear()), 4).substr(2);
  118. },
  119. yyyy: function(dateObj) {
  120. return pad(dateObj.getFullYear(), 4);
  121. },
  122. h: function(dateObj) {
  123. return dateObj.getHours() % 12 || 12;
  124. },
  125. hh: function(dateObj) {
  126. return pad(dateObj.getHours() % 12 || 12);
  127. },
  128. H: function(dateObj) {
  129. return dateObj.getHours();
  130. },
  131. HH: function(dateObj) {
  132. return pad(dateObj.getHours());
  133. },
  134. m: function(dateObj) {
  135. return dateObj.getMinutes();
  136. },
  137. mm: function(dateObj) {
  138. return pad(dateObj.getMinutes());
  139. },
  140. s: function(dateObj) {
  141. return dateObj.getSeconds();
  142. },
  143. ss: function(dateObj) {
  144. return pad(dateObj.getSeconds());
  145. },
  146. S: function(dateObj) {
  147. return Math.round(dateObj.getMilliseconds() / 100);
  148. },
  149. SS: function(dateObj) {
  150. return pad(Math.round(dateObj.getMilliseconds() / 10), 2);
  151. },
  152. SSS: function(dateObj) {
  153. return pad(dateObj.getMilliseconds(), 3);
  154. },
  155. a: function(dateObj, i18n) {
  156. return dateObj.getHours() < 12 ? i18n.amPm[0] : i18n.amPm[1];
  157. },
  158. A: function(dateObj, i18n) {
  159. return dateObj.getHours() < 12 ? i18n.amPm[0].toUpperCase() : i18n.amPm[1].toUpperCase();
  160. },
  161. ZZ: function(dateObj) {
  162. var o = dateObj.getTimezoneOffset();
  163. return (o > 0 ? '-' : '+') + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4);
  164. }
  165. };
  166. var parseFlags = {
  167. d: [twoDigits, function (d, v) {
  168. d.day = v;
  169. }],
  170. Do: [twoDigits + word, function (d, v) {
  171. d.day = parseInt(v, 10);
  172. }],
  173. M: [twoDigits, function (d, v) {
  174. d.month = v - 1;
  175. }],
  176. yy: [twoDigits, function (d, v) {
  177. var da = new Date(), cent = +('' + da.getFullYear()).substr(0, 2);
  178. d.year = '' + (v > 68 ? cent - 1 : cent) + v;
  179. }],
  180. h: [twoDigits, function (d, v) {
  181. d.hour = v;
  182. }],
  183. m: [twoDigits, function (d, v) {
  184. d.minute = v;
  185. }],
  186. s: [twoDigits, function (d, v) {
  187. d.second = v;
  188. }],
  189. yyyy: [fourDigits, function (d, v) {
  190. d.year = v;
  191. }],
  192. S: ['\\d', function (d, v) {
  193. d.millisecond = v * 100;
  194. }],
  195. SS: ['\\d{2}', function (d, v) {
  196. d.millisecond = v * 10;
  197. }],
  198. SSS: [threeDigits, function (d, v) {
  199. d.millisecond = v;
  200. }],
  201. D: [twoDigits, noop],
  202. ddd: [word, noop],
  203. MMM: [word, monthUpdate('monthNamesShort')],
  204. MMMM: [word, monthUpdate('monthNames')],
  205. a: [word, function (d, v, i18n) {
  206. var val = v.toLowerCase();
  207. if (val === i18n.amPm[0]) {
  208. d.isPm = false;
  209. } else if (val === i18n.amPm[1]) {
  210. d.isPm = true;
  211. }
  212. }],
  213. ZZ: ['[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z', function (d, v) {
  214. var parts = (v + '').match(/([+-]|\d\d)/gi), minutes;
  215. if (parts) {
  216. minutes = +(parts[1] * 60) + parseInt(parts[2], 10);
  217. d.timezoneOffset = parts[0] === '+' ? minutes : -minutes;
  218. }
  219. }]
  220. };
  221. parseFlags.dd = parseFlags.d;
  222. parseFlags.dddd = parseFlags.ddd;
  223. parseFlags.DD = parseFlags.D;
  224. parseFlags.mm = parseFlags.m;
  225. parseFlags.hh = parseFlags.H = parseFlags.HH = parseFlags.h;
  226. parseFlags.MM = parseFlags.M;
  227. parseFlags.ss = parseFlags.s;
  228. parseFlags.A = parseFlags.a;
  229. // Some common format strings
  230. fecha.masks = {
  231. default: 'ddd MMM dd yyyy HH:mm:ss',
  232. shortDate: 'M/D/yy',
  233. mediumDate: 'MMM d, yyyy',
  234. longDate: 'MMMM d, yyyy',
  235. fullDate: 'dddd, MMMM d, yyyy',
  236. shortTime: 'HH:mm',
  237. mediumTime: 'HH:mm:ss',
  238. longTime: 'HH:mm:ss.SSS'
  239. };
  240. /***
  241. * Format a date
  242. * @method format
  243. * @param {Date|number} dateObj
  244. * @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
  245. */
  246. fecha.format = function (dateObj, mask, i18nSettings) {
  247. var i18n = i18nSettings || fecha.i18n;
  248. if (typeof dateObj === 'number') {
  249. dateObj = new Date(dateObj);
  250. }
  251. if (Object.prototype.toString.call(dateObj) !== '[object Date]' || isNaN(dateObj.getTime())) {
  252. throw new Error('Invalid Date in fecha.format');
  253. }
  254. mask = fecha.masks[mask] || mask || fecha.masks['default'];
  255. var literals = [];
  256. // Make literals inactive by replacing them with ??
  257. mask = mask.replace(literal, function($0, $1) {
  258. literals.push($1);
  259. return '@@@';
  260. });
  261. // Apply formatting rules
  262. mask = mask.replace(token, function ($0) {
  263. return $0 in formatFlags ? formatFlags[$0](dateObj, i18n) : $0.slice(1, $0.length - 1);
  264. });
  265. // Inline literal values back into the formatted value
  266. return mask.replace(/@@@/g, function() {
  267. return literals.shift();
  268. });
  269. };
  270. /**
  271. * Parse a date string into an object, changes - into /
  272. * @method parse
  273. * @param {string} dateStr Date string
  274. * @param {string} format Date parse format
  275. * @returns {Date|boolean}
  276. */
  277. fecha.parse = function (dateStr, format, i18nSettings) {
  278. var i18n = i18nSettings || fecha.i18n;
  279. if (typeof format !== 'string') {
  280. throw new Error('Invalid format in fecha.parse');
  281. }
  282. format = fecha.masks[format] || format;
  283. // Avoid regular expression denial of service, fail early for really long strings
  284. // https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS
  285. if (dateStr.length > 1000) {
  286. return null;
  287. }
  288. var dateInfo = {};
  289. var parseInfo = [];
  290. var literals = [];
  291. format = format.replace(literal, function($0, $1) {
  292. literals.push($1);
  293. return '@@@';
  294. });
  295. var newFormat = regexEscape(format).replace(token, function ($0) {
  296. if (parseFlags[$0]) {
  297. var info = parseFlags[$0];
  298. parseInfo.push(info[1]);
  299. return '(' + info[0] + ')';
  300. }
  301. return $0;
  302. });
  303. newFormat = newFormat.replace(/@@@/g, function() {
  304. return literals.shift();
  305. });
  306. var matches = dateStr.match(new RegExp(newFormat, 'i'));
  307. if (!matches) {
  308. return null;
  309. }
  310. for (var i = 1; i < matches.length; i++) {
  311. parseInfo[i - 1](dateInfo, matches[i], i18n);
  312. }
  313. var today = new Date();
  314. if (dateInfo.isPm === true && dateInfo.hour != null && +dateInfo.hour !== 12) {
  315. dateInfo.hour = +dateInfo.hour + 12;
  316. } else if (dateInfo.isPm === false && +dateInfo.hour === 12) {
  317. dateInfo.hour = 0;
  318. }
  319. var date;
  320. if (dateInfo.timezoneOffset != null) {
  321. dateInfo.minute = +(dateInfo.minute || 0) - +dateInfo.timezoneOffset;
  322. date = new Date(Date.UTC(dateInfo.year || today.getFullYear(), dateInfo.month || 0, dateInfo.day || 1,
  323. dateInfo.hour || 0, dateInfo.minute || 0, dateInfo.second || 0, dateInfo.millisecond || 0));
  324. } else {
  325. date = new Date(dateInfo.year || today.getFullYear(), dateInfo.month || 0, dateInfo.day || 1,
  326. dateInfo.hour || 0, dateInfo.minute || 0, dateInfo.second || 0, dateInfo.millisecond || 0);
  327. }
  328. return date;
  329. };
  330. /* istanbul ignore next */
  331. if (typeof module !== 'undefined' && module.exports) {
  332. module.exports = fecha;
  333. } else if (typeof define === 'function' && define.amd) {
  334. define(function () {
  335. return fecha;
  336. });
  337. } else {
  338. main.fecha = fecha;
  339. }
  340. })(this);