fecha.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. (function (main) {
  2. 'use strict';
  3. /**
  4. * Parse or format dates
  5. * @class fecha
  6. */
  7. var fecha = {};
  8. var token = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
  9. var twoDigits = /\d\d?/;
  10. var threeDigits = /\d{3}/;
  11. var fourDigits = /\d{4}/;
  12. var word = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i;
  13. var literal = /\[([^]*?)\]/gm;
  14. var noop = function () {
  15. };
  16. function shorten(arr, sLen) {
  17. var newArr = [];
  18. for (var i = 0, len = arr.length; i < len; i++) {
  19. newArr.push(arr[i].substr(0, sLen));
  20. }
  21. return newArr;
  22. }
  23. function monthUpdate(arrName) {
  24. return function (d, v, i18n) {
  25. var index = i18n[arrName].indexOf(v.charAt(0).toUpperCase() + v.substr(1).toLowerCase());
  26. if (~index) {
  27. d.month = index;
  28. }
  29. };
  30. }
  31. function pad(val, len) {
  32. val = String(val);
  33. len = len || 2;
  34. while (val.length < len) {
  35. val = '0' + val;
  36. }
  37. return val;
  38. }
  39. var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  40. var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  41. var monthNamesShort = shorten(monthNames, 3);
  42. var dayNamesShort = shorten(dayNames, 3);
  43. fecha.i18n = {
  44. dayNamesShort: dayNamesShort,
  45. dayNames: dayNames,
  46. monthNamesShort: monthNamesShort,
  47. monthNames: monthNames,
  48. amPm: ['am', 'pm'],
  49. DoFn: function DoFn(D) {
  50. return D + ['th', 'st', 'nd', 'rd'][D % 10 > 3 ? 0 : (D - D % 10 !== 10) * D % 10];
  51. }
  52. };
  53. var formatFlags = {
  54. D: function(dateObj) {
  55. return dateObj.getDate();
  56. },
  57. DD: function(dateObj) {
  58. return pad(dateObj.getDate());
  59. },
  60. Do: function(dateObj, i18n) {
  61. return i18n.DoFn(dateObj.getDate());
  62. },
  63. d: function(dateObj) {
  64. return dateObj.getDay();
  65. },
  66. dd: function(dateObj) {
  67. return pad(dateObj.getDay());
  68. },
  69. ddd: function(dateObj, i18n) {
  70. return i18n.dayNamesShort[dateObj.getDay()];
  71. },
  72. dddd: function(dateObj, i18n) {
  73. return i18n.dayNames[dateObj.getDay()];
  74. },
  75. M: function(dateObj) {
  76. return dateObj.getMonth() + 1;
  77. },
  78. MM: function(dateObj) {
  79. return pad(dateObj.getMonth() + 1);
  80. },
  81. MMM: function(dateObj, i18n) {
  82. return i18n.monthNamesShort[dateObj.getMonth()];
  83. },
  84. MMMM: function(dateObj, i18n) {
  85. return i18n.monthNames[dateObj.getMonth()];
  86. },
  87. YY: function(dateObj) {
  88. return String(dateObj.getFullYear()).substr(2);
  89. },
  90. YYYY: function(dateObj) {
  91. return pad(dateObj.getFullYear(), 4);
  92. },
  93. h: function(dateObj) {
  94. return dateObj.getHours() % 12 || 12;
  95. },
  96. hh: function(dateObj) {
  97. return pad(dateObj.getHours() % 12 || 12);
  98. },
  99. H: function(dateObj) {
  100. return dateObj.getHours();
  101. },
  102. HH: function(dateObj) {
  103. return pad(dateObj.getHours());
  104. },
  105. m: function(dateObj) {
  106. return dateObj.getMinutes();
  107. },
  108. mm: function(dateObj) {
  109. return pad(dateObj.getMinutes());
  110. },
  111. s: function(dateObj) {
  112. return dateObj.getSeconds();
  113. },
  114. ss: function(dateObj) {
  115. return pad(dateObj.getSeconds());
  116. },
  117. S: function(dateObj) {
  118. return Math.round(dateObj.getMilliseconds() / 100);
  119. },
  120. SS: function(dateObj) {
  121. return pad(Math.round(dateObj.getMilliseconds() / 10), 2);
  122. },
  123. SSS: function(dateObj) {
  124. return pad(dateObj.getMilliseconds(), 3);
  125. },
  126. a: function(dateObj, i18n) {
  127. return dateObj.getHours() < 12 ? i18n.amPm[0] : i18n.amPm[1];
  128. },
  129. A: function(dateObj, i18n) {
  130. return dateObj.getHours() < 12 ? i18n.amPm[0].toUpperCase() : i18n.amPm[1].toUpperCase();
  131. },
  132. ZZ: function(dateObj) {
  133. var o = dateObj.getTimezoneOffset();
  134. return (o > 0 ? '-' : '+') + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4);
  135. }
  136. };
  137. var parseFlags = {
  138. D: [twoDigits, function (d, v) {
  139. d.day = v;
  140. }],
  141. Do: [new RegExp(twoDigits.source + word.source), function (d, v) {
  142. d.day = parseInt(v, 10);
  143. }],
  144. M: [twoDigits, function (d, v) {
  145. d.month = v - 1;
  146. }],
  147. YY: [twoDigits, function (d, v) {
  148. var da = new Date(), cent = +('' + da.getFullYear()).substr(0, 2);
  149. d.year = '' + (v > 68 ? cent - 1 : cent) + v;
  150. }],
  151. h: [twoDigits, function (d, v) {
  152. d.hour = v;
  153. }],
  154. m: [twoDigits, function (d, v) {
  155. d.minute = v;
  156. }],
  157. s: [twoDigits, function (d, v) {
  158. d.second = v;
  159. }],
  160. YYYY: [fourDigits, function (d, v) {
  161. d.year = v;
  162. }],
  163. S: [/\d/, function (d, v) {
  164. d.millisecond = v * 100;
  165. }],
  166. SS: [/\d{2}/, function (d, v) {
  167. d.millisecond = v * 10;
  168. }],
  169. SSS: [threeDigits, function (d, v) {
  170. d.millisecond = v;
  171. }],
  172. d: [twoDigits, noop],
  173. ddd: [word, noop],
  174. MMM: [word, monthUpdate('monthNamesShort')],
  175. MMMM: [word, monthUpdate('monthNames')],
  176. a: [word, function (d, v, i18n) {
  177. var val = v.toLowerCase();
  178. if (val === i18n.amPm[0]) {
  179. d.isPm = false;
  180. } else if (val === i18n.amPm[1]) {
  181. d.isPm = true;
  182. }
  183. }],
  184. ZZ: [/([\+\-]\d\d:?\d\d|Z)/, function (d, v) {
  185. if (v === 'Z') v = '+00:00';
  186. var parts = (v + '').match(/([\+\-]|\d\d)/gi), minutes;
  187. if (parts) {
  188. minutes = +(parts[1] * 60) + parseInt(parts[2], 10);
  189. d.timezoneOffset = parts[0] === '+' ? minutes : -minutes;
  190. }
  191. }]
  192. };
  193. parseFlags.dd = parseFlags.d;
  194. parseFlags.dddd = parseFlags.ddd;
  195. parseFlags.DD = parseFlags.D;
  196. parseFlags.mm = parseFlags.m;
  197. parseFlags.hh = parseFlags.H = parseFlags.HH = parseFlags.h;
  198. parseFlags.MM = parseFlags.M;
  199. parseFlags.ss = parseFlags.s;
  200. parseFlags.A = parseFlags.a;
  201. // Some common format strings
  202. fecha.masks = {
  203. default: 'ddd MMM DD YYYY HH:mm:ss',
  204. shortDate: 'M/D/YY',
  205. mediumDate: 'MMM D, YYYY',
  206. longDate: 'MMMM D, YYYY',
  207. fullDate: 'dddd, MMMM D, YYYY',
  208. shortTime: 'HH:mm',
  209. mediumTime: 'HH:mm:ss',
  210. longTime: 'HH:mm:ss.SSS'
  211. };
  212. /***
  213. * Format a date
  214. * @method format
  215. * @param {Date|number} dateObj
  216. * @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
  217. */
  218. fecha.format = function (dateObj, mask, i18nSettings) {
  219. var i18n = i18nSettings || fecha.i18n;
  220. if (typeof dateObj === 'number') {
  221. dateObj = new Date(dateObj);
  222. }
  223. if (Object.prototype.toString.call(dateObj) !== '[object Date]' || isNaN(dateObj.getTime())) {
  224. throw new Error('Invalid Date in fecha.format');
  225. }
  226. mask = fecha.masks[mask] || mask || fecha.masks['default'];
  227. var literals = [];
  228. // Make literals inactive by replacing them with ??
  229. mask = mask.replace(literal, function($0, $1) {
  230. literals.push($1);
  231. return '??';
  232. });
  233. // Apply formatting rules
  234. mask = mask.replace(token, function ($0) {
  235. return $0 in formatFlags ? formatFlags[$0](dateObj, i18n) : $0.slice(1, $0.length - 1);
  236. });
  237. // Inline literal values back into the formatted value
  238. return mask.replace(/\?\?/g, function() {
  239. return literals.shift();
  240. });
  241. };
  242. /**
  243. * Parse a date string into an object, changes - into /
  244. * @method parse
  245. * @param {string} dateStr Date string
  246. * @param {string} format Date parse format
  247. * @returns {Date|boolean}
  248. */
  249. fecha.parse = function (dateStr, format, i18nSettings) {
  250. var i18n = i18nSettings || fecha.i18n;
  251. if (typeof format !== 'string') {
  252. throw new Error('Invalid format in fecha.parse');
  253. }
  254. format = fecha.masks[format] || format;
  255. // Avoid regular expression denial of service, fail early for really long strings
  256. // https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS
  257. if (dateStr.length > 1000) {
  258. return false;
  259. }
  260. var isValid = true;
  261. var dateInfo = {};
  262. format.replace(token, function ($0) {
  263. if (parseFlags[$0]) {
  264. var info = parseFlags[$0];
  265. var index = dateStr.search(info[0]);
  266. if (!~index) {
  267. isValid = false;
  268. } else {
  269. dateStr.replace(info[0], function (result) {
  270. info[1](dateInfo, result, i18n);
  271. dateStr = dateStr.substr(index + result.length);
  272. return result;
  273. });
  274. }
  275. }
  276. return parseFlags[$0] ? '' : $0.slice(1, $0.length - 1);
  277. });
  278. if (!isValid) {
  279. return false;
  280. }
  281. var today = new Date();
  282. if (dateInfo.isPm === true && dateInfo.hour != null && +dateInfo.hour !== 12) {
  283. dateInfo.hour = +dateInfo.hour + 12;
  284. } else if (dateInfo.isPm === false && +dateInfo.hour === 12) {
  285. dateInfo.hour = 0;
  286. }
  287. var date;
  288. if (dateInfo.timezoneOffset != null) {
  289. dateInfo.minute = +(dateInfo.minute || 0) - +dateInfo.timezoneOffset;
  290. date = new Date(Date.UTC(dateInfo.year || today.getFullYear(), dateInfo.month || 0, dateInfo.day || 1,
  291. dateInfo.hour || 0, dateInfo.minute || 0, dateInfo.second || 0, dateInfo.millisecond || 0));
  292. } else {
  293. date = new Date(dateInfo.year || today.getFullYear(), dateInfo.month || 0, dateInfo.day || 1,
  294. dateInfo.hour || 0, dateInfo.minute || 0, dateInfo.second || 0, dateInfo.millisecond || 0);
  295. }
  296. return date;
  297. };
  298. /* istanbul ignore next */
  299. if (typeof module !== 'undefined' && module.exports) {
  300. module.exports = fecha;
  301. } else if (typeof define === 'function' && define.amd) {
  302. define(function () {
  303. return fecha;
  304. });
  305. } else {
  306. main.fecha = fecha;
  307. }
  308. })(this);