moment.js 4.9 KB

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