dateFns.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _dateFns = require("date-fns");
  8. var locales = _interopRequireWildcard(require("date-fns/locale"));
  9. var getLocale = function getLocale(locale) {
  10. return locales[locale] || locales[locale.replace(/_/g, '')] || locales[locale.replace(/_.*$/g, '')];
  11. };
  12. var localeParse = function localeParse(format) {
  13. return format.replace(/Y/g, 'y').replace(/D/g, 'd').replace(/gggg/, 'yyyy').replace(/g/g, 'G').replace(/([Ww])o/g, 'wo');
  14. };
  15. var _parse = function parse(text, format, locale) {
  16. return (0, _dateFns.parse)(text, localeParse(format), new Date(), {
  17. locale: getLocale(locale)
  18. });
  19. };
  20. /**
  21. * Check if the text is a valid date considering the format and locale
  22. *
  23. * This is a strict check, the date string must match the format exactly.
  24. * Date-fns allows some flexibility in parsing dates, for example, it will parse "30/01/2" as "30/01/002".
  25. * This behavior is not desirable in our case, so we need to check if the date string matches the format exactly.
  26. *
  27. * @param text the date string
  28. * @param format the date format to use
  29. * @param locale the locale to use
  30. */
  31. var isStrictValidDate = function isStrictValidDate(text, format, locale) {
  32. var date = _parse(text, format, locale);
  33. if (!(0, _dateFns.isValid)(date)) {
  34. return false;
  35. }
  36. var formattedDate = (0, _dateFns.format)(date, format, {
  37. locale: getLocale(locale)
  38. });
  39. return text === formattedDate;
  40. };
  41. var generateConfig = {
  42. // get
  43. getNow: function getNow() {
  44. return new Date();
  45. },
  46. getFixedDate: function getFixedDate(string) {
  47. return new Date(string);
  48. },
  49. getEndDate: function getEndDate(date) {
  50. return (0, _dateFns.endOfMonth)(date);
  51. },
  52. getWeekDay: function getWeekDay(date) {
  53. return (0, _dateFns.getDay)(date);
  54. },
  55. getYear: function getYear(date) {
  56. return (0, _dateFns.getYear)(date);
  57. },
  58. getMonth: function getMonth(date) {
  59. return (0, _dateFns.getMonth)(date);
  60. },
  61. getDate: function getDate(date) {
  62. return (0, _dateFns.getDate)(date);
  63. },
  64. getHour: function getHour(date) {
  65. return (0, _dateFns.getHours)(date);
  66. },
  67. getMinute: function getMinute(date) {
  68. return (0, _dateFns.getMinutes)(date);
  69. },
  70. getSecond: function getSecond(date) {
  71. return (0, _dateFns.getSeconds)(date);
  72. },
  73. getMillisecond: function getMillisecond(date) {
  74. return (0, _dateFns.getMilliseconds)(date);
  75. },
  76. // set
  77. addYear: function addYear(date, diff) {
  78. return (0, _dateFns.addYears)(date, diff);
  79. },
  80. addMonth: function addMonth(date, diff) {
  81. return (0, _dateFns.addMonths)(date, diff);
  82. },
  83. addDate: function addDate(date, diff) {
  84. return (0, _dateFns.addDays)(date, diff);
  85. },
  86. setYear: function setYear(date, year) {
  87. return (0, _dateFns.setYear)(date, year);
  88. },
  89. setMonth: function setMonth(date, month) {
  90. return (0, _dateFns.setMonth)(date, month);
  91. },
  92. setDate: function setDate(date, num) {
  93. return (0, _dateFns.setDate)(date, num);
  94. },
  95. setHour: function setHour(date, hour) {
  96. return (0, _dateFns.setHours)(date, hour);
  97. },
  98. setMinute: function setMinute(date, minute) {
  99. return (0, _dateFns.setMinutes)(date, minute);
  100. },
  101. setSecond: function setSecond(date, second) {
  102. return (0, _dateFns.setSeconds)(date, second);
  103. },
  104. setMillisecond: function setMillisecond(date, millisecond) {
  105. return (0, _dateFns.setMilliseconds)(date, millisecond);
  106. },
  107. // Compare
  108. isAfter: function isAfter(date1, date2) {
  109. return (0, _dateFns.isAfter)(date1, date2);
  110. },
  111. isValidate: function isValidate(date) {
  112. return (0, _dateFns.isValid)(date);
  113. },
  114. locale: {
  115. getWeekFirstDay: function getWeekFirstDay(locale) {
  116. var clone = getLocale(locale);
  117. return clone.options.weekStartsOn;
  118. },
  119. getWeekFirstDate: function getWeekFirstDate(locale, date) {
  120. return (0, _dateFns.startOfWeek)(date, {
  121. locale: getLocale(locale)
  122. });
  123. },
  124. getWeek: function getWeek(locale, date) {
  125. return (0, _dateFns.getWeek)(date, {
  126. locale: getLocale(locale)
  127. });
  128. },
  129. getShortWeekDays: function getShortWeekDays(locale) {
  130. var clone = getLocale(locale);
  131. return Array.from({
  132. length: 7
  133. }).map(function (_, i) {
  134. return clone.localize.day(i, {
  135. width: 'short'
  136. });
  137. });
  138. },
  139. getShortMonths: function getShortMonths(locale) {
  140. var clone = getLocale(locale);
  141. return Array.from({
  142. length: 12
  143. }).map(function (_, i) {
  144. return clone.localize.month(i, {
  145. width: 'abbreviated'
  146. });
  147. });
  148. },
  149. format: function format(locale, date, _format) {
  150. if (!(0, _dateFns.isValid)(date)) {
  151. return null;
  152. }
  153. return (0, _dateFns.format)(date, localeParse(_format), {
  154. locale: getLocale(locale)
  155. });
  156. },
  157. parse: function parse(locale, text, formats) {
  158. for (var i = 0; i < formats.length; i += 1) {
  159. var format = localeParse(formats[i]);
  160. if (isStrictValidDate(text, format, locale)) {
  161. return _parse(text, format, locale);
  162. }
  163. }
  164. return null;
  165. }
  166. }
  167. };
  168. var _default = exports.default = generateConfig;