dateFns.js 5.0 KB

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