moment.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { noteOnce } from '../../vc-util/warning';
  2. import moment from 'moment';
  3. const generateConfig = {
  4. // get
  5. getNow: () => moment(),
  6. getFixedDate: string => moment(string, 'YYYY-MM-DD'),
  7. getEndDate: date => {
  8. const clone = date.clone();
  9. return clone.endOf('month');
  10. },
  11. getWeekDay: date => {
  12. const clone = date.clone().locale('en_US');
  13. return clone.weekday() + clone.localeData().firstDayOfWeek();
  14. },
  15. getYear: date => date.year(),
  16. getMonth: date => date.month(),
  17. getDate: date => date.date(),
  18. getHour: date => date.hour(),
  19. getMinute: date => date.minute(),
  20. getSecond: date => date.second(),
  21. // set
  22. addYear: (date, diff) => {
  23. const clone = date.clone();
  24. return clone.add(diff, 'year');
  25. },
  26. addMonth: (date, diff) => {
  27. const clone = date.clone();
  28. return clone.add(diff, 'month');
  29. },
  30. addDate: (date, diff) => {
  31. const clone = date.clone();
  32. return clone.add(diff, 'day');
  33. },
  34. setYear: (date, year) => {
  35. const clone = date.clone();
  36. return clone.year(year);
  37. },
  38. setMonth: (date, month) => {
  39. const clone = date.clone();
  40. return clone.month(month);
  41. },
  42. setDate: (date, num) => {
  43. const clone = date.clone();
  44. return clone.date(num);
  45. },
  46. setHour: (date, hour) => {
  47. const clone = date.clone();
  48. return clone.hour(hour);
  49. },
  50. setMinute: (date, minute) => {
  51. const clone = date.clone();
  52. return clone.minute(minute);
  53. },
  54. setSecond: (date, second) => {
  55. const clone = date.clone();
  56. return clone.second(second);
  57. },
  58. // Compare
  59. isAfter: (date1, date2) => date1.isAfter(date2),
  60. isValidate: date => date.isValid(),
  61. locale: {
  62. getWeekFirstDay: locale => {
  63. const date = moment().locale(locale);
  64. return date.localeData().firstDayOfWeek();
  65. },
  66. getWeekFirstDate: (locale, date) => {
  67. const clone = date.clone();
  68. const result = clone.locale(locale);
  69. return result.weekday(0);
  70. },
  71. getWeek: (locale, date) => {
  72. const clone = date.clone();
  73. const result = clone.locale(locale);
  74. return result.week();
  75. },
  76. getShortWeekDays: locale => {
  77. const date = moment().locale(locale);
  78. return date.localeData().weekdaysMin();
  79. },
  80. getShortMonths: locale => {
  81. const date = moment().locale(locale);
  82. return date.localeData().monthsShort();
  83. },
  84. format: (locale, date, format) => {
  85. const clone = date.clone();
  86. const result = clone.locale(locale);
  87. return result.format(format);
  88. },
  89. parse: (locale, text, formats) => {
  90. const fallbackFormatList = [];
  91. for (let i = 0; i < formats.length; i += 1) {
  92. let format = formats[i];
  93. let formatText = text;
  94. if (format.includes('wo') || format.includes('Wo')) {
  95. format = format.replace(/wo/g, 'w').replace(/Wo/g, 'W');
  96. const matchFormat = format.match(/[-YyMmDdHhSsWwGg]+/g);
  97. const matchText = formatText.match(/[-\d]+/g);
  98. if (matchFormat && matchText) {
  99. format = matchFormat.join('');
  100. formatText = matchText.join('');
  101. } else {
  102. fallbackFormatList.push(format.replace(/o/g, ''));
  103. }
  104. }
  105. const date = moment(formatText, format, locale, true);
  106. if (date.isValid()) {
  107. return date;
  108. }
  109. }
  110. // Fallback to fuzzy matching, this should always not reach match or need fire a issue
  111. for (let i = 0; i < fallbackFormatList.length; i += 1) {
  112. const date = moment(text, fallbackFormatList[i], locale, false);
  113. /* istanbul ignore next */
  114. if (date.isValid()) {
  115. noteOnce(false, 'Not match any format strictly and fallback to fuzzy match. Please help to fire a issue about this.');
  116. return date;
  117. }
  118. }
  119. return null;
  120. }
  121. },
  122. toDate: (value, valueFormat) => {
  123. if (Array.isArray(value)) {
  124. return value.map(val => typeof val === 'string' && val ? moment(val, valueFormat) : val || null);
  125. } else {
  126. return typeof value === 'string' && value ? moment(value, valueFormat) : value || null;
  127. }
  128. },
  129. toString: (value, valueFormat) => {
  130. if (Array.isArray(value)) {
  131. return value.map(val => moment.isMoment(val) ? val.format(valueFormat) : val);
  132. } else {
  133. return moment.isMoment(value) ? value.format(valueFormat) : value;
  134. }
  135. }
  136. };
  137. export default generateConfig;