dateFns.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { getDay, getYear as _getYear, getMonth as _getMonth, getDate as _getDate, endOfMonth, getHours, getMinutes, getSeconds, addYears, addMonths, addDays, setYear as _setYear, setMonth as _setMonth, setDate as _setDate, setHours, setMinutes, setSeconds, isAfter as _isAfter, isValid, getWeek as _getWeek, startOfWeek, format as formatDate, parse as parseDate, isDate, isMatch } from 'date-fns';
  2. import * as Locale from 'date-fns/locale';
  3. var dealLocal = function dealLocal(str) {
  4. return str.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 generateConfig = {
  10. // get
  11. getNow: function getNow() {
  12. return new Date();
  13. },
  14. getFixedDate: function getFixedDate(string) {
  15. return new Date(string);
  16. },
  17. getEndDate: function getEndDate(date) {
  18. return endOfMonth(date);
  19. },
  20. getWeekDay: function getWeekDay(date) {
  21. return getDay(date);
  22. },
  23. getYear: function getYear(date) {
  24. return _getYear(date);
  25. },
  26. getMonth: function getMonth(date) {
  27. return _getMonth(date);
  28. },
  29. getDate: function getDate(date) {
  30. return _getDate(date);
  31. },
  32. getHour: function getHour(date) {
  33. return getHours(date);
  34. },
  35. getMinute: function getMinute(date) {
  36. return getMinutes(date);
  37. },
  38. getSecond: function getSecond(date) {
  39. return getSeconds(date);
  40. },
  41. // set
  42. addYear: function addYear(date, diff) {
  43. return addYears(date, diff);
  44. },
  45. addMonth: function addMonth(date, diff) {
  46. return addMonths(date, diff);
  47. },
  48. addDate: function addDate(date, diff) {
  49. return addDays(date, diff);
  50. },
  51. setYear: function setYear(date, year) {
  52. return _setYear(date, year);
  53. },
  54. setMonth: function setMonth(date, month) {
  55. return _setMonth(date, month);
  56. },
  57. setDate: function setDate(date, num) {
  58. return _setDate(date, num);
  59. },
  60. setHour: function setHour(date, hour) {
  61. return setHours(date, hour);
  62. },
  63. setMinute: function setMinute(date, minute) {
  64. return setMinutes(date, minute);
  65. },
  66. setSecond: function setSecond(date, second) {
  67. return setSeconds(date, second);
  68. },
  69. // Compare
  70. isAfter: function isAfter(date1, date2) {
  71. return _isAfter(date1, date2);
  72. },
  73. isValidate: function isValidate(date) {
  74. return isValid(date);
  75. },
  76. locale: {
  77. getWeekFirstDay: function getWeekFirstDay(locale) {
  78. var clone = Locale[dealLocal(locale)];
  79. return clone.options.weekStartsOn;
  80. },
  81. getWeekFirstDate: function getWeekFirstDate(locale, date) {
  82. return startOfWeek(date, {
  83. locale: Locale[dealLocal(locale)]
  84. });
  85. },
  86. getWeek: function getWeek(locale, date) {
  87. return _getWeek(date, {
  88. locale: Locale[dealLocal(locale)]
  89. });
  90. },
  91. getShortWeekDays: function getShortWeekDays(locale) {
  92. var clone = Locale[dealLocal(locale)];
  93. return Array.from({
  94. length: 7
  95. }).map(function (_, i) {
  96. return clone.localize.day(i, {
  97. width: 'short'
  98. });
  99. });
  100. },
  101. getShortMonths: function getShortMonths(locale) {
  102. var clone = Locale[dealLocal(locale)];
  103. return Array.from({
  104. length: 12
  105. }).map(function (_, i) {
  106. return clone.localize.month(i, {
  107. width: 'abbreviated'
  108. });
  109. });
  110. },
  111. format: function format(locale, date, _format) {
  112. if (!isValid(date)) {
  113. return null;
  114. }
  115. return formatDate(date, localeParse(_format), {
  116. locale: Locale[dealLocal(locale)]
  117. });
  118. },
  119. parse: function parse(locale, text, formats) {
  120. for (var i = 0; i < formats.length; i += 1) {
  121. var format = localeParse(formats[i]);
  122. var formatText = text;
  123. var date = parseDate(formatText, format, new Date(), {
  124. locale: Locale[dealLocal(locale)]
  125. });
  126. if (isValid(date) && formatText.length === format.length && isMatch(formatText, format)) {
  127. return date;
  128. }
  129. }
  130. return null;
  131. }
  132. },
  133. toDate: function toDate(value, valueFormat) {
  134. if (Array.isArray(value)) {
  135. return value.map(function (val) {
  136. return typeof val === 'string' && val ? parseDate(val, valueFormat, new Date()) : val || null;
  137. });
  138. } else {
  139. return typeof value === 'string' && value ? parseDate(value, valueFormat, new Date()) : value || null;
  140. }
  141. },
  142. toString: function toString(value, valueFormat) {
  143. if (Array.isArray(value)) {
  144. return value.map(function (val) {
  145. return isDate(val) ? formatDate(val, valueFormat) : val;
  146. });
  147. } else {
  148. return isDate(value) ? formatDate(value, valueFormat) : value;
  149. }
  150. }
  151. };
  152. export default generateConfig;