util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { __spreadArray } from "tslib";
  2. import dayjs from 'dayjs';
  3. function getArray(start, end, format) {
  4. var res = [];
  5. for (var i = 0; i < end - start + 1; i++) {
  6. res.push({ label: format(start + i), value: start + i });
  7. }
  8. return res;
  9. }
  10. var precisionLengthRecord = {
  11. year: 1,
  12. month: 2,
  13. day: 3,
  14. hour: 4,
  15. minute: 5,
  16. second: 6,
  17. };
  18. function getYears(min, max, format) {
  19. return getArray(min.year(), max.year(), format.bind(this, 'year'));
  20. }
  21. function getMonths(min, max, currentPicker, format) {
  22. var start = 1;
  23. var end = 12;
  24. if (currentPicker
  25. .clone()
  26. .set('month', start - 1)
  27. .isBefore(min)) {
  28. start = min.month() + 1;
  29. }
  30. if (currentPicker
  31. .clone()
  32. .set('month', end - 1)
  33. .isAfter(max)) {
  34. end = max.month() + 1;
  35. }
  36. return getArray(start, end, format.bind(this, 'month'));
  37. }
  38. function getDates(min, max, currentPicker, format) {
  39. var start = 1;
  40. var end = currentPicker.daysInMonth();
  41. if (currentPicker.clone().set('date', start).isBefore(min)) {
  42. start = min.date();
  43. }
  44. if (currentPicker.clone().set('date', end).isAfter(max)) {
  45. end = max.date();
  46. }
  47. return getArray(start, end, format.bind(this, 'day'));
  48. }
  49. function getHours(min, max, currentPicker, format) {
  50. var start = 0;
  51. var end = 23;
  52. if (currentPicker.clone().set('hour', start).isBefore(min)) {
  53. start = min.hour();
  54. }
  55. if (currentPicker.clone().set('hour', end).isAfter(max)) {
  56. end = max.hour();
  57. }
  58. return getArray(start, end, format.bind(this, 'hour'));
  59. }
  60. function getMinutes(min, max, currentPicker, format) {
  61. var start = 0;
  62. var end = 59;
  63. if (currentPicker.clone().set('minute', start).isBefore(min)) {
  64. start = min.minute();
  65. }
  66. if (currentPicker.clone().set('minute', end).isAfter(max)) {
  67. end = max.minute();
  68. }
  69. return getArray(start, end, format.bind(this, 'minute'));
  70. }
  71. function getSeconds(min, max, currentPicker, format) {
  72. var start = 0;
  73. var end = 59;
  74. if (currentPicker.clone().set('second', start).isBefore(min)) {
  75. start = min.second();
  76. }
  77. if (currentPicker.clone().set('second', end).isAfter(max)) {
  78. end = max.second();
  79. }
  80. return getArray(start, end, format.bind(this, 'second'));
  81. }
  82. export function getRangeData(precision, min, max, currentPickerDay, format) {
  83. var data = [];
  84. var len = precisionLengthRecord[precision];
  85. if (!len)
  86. return [];
  87. for (var i = 0; i < len; i++) {
  88. switch (i) {
  89. case 0:
  90. data.push(getYears(min, max, format));
  91. break;
  92. case 1:
  93. data.push(getMonths(min, max, currentPickerDay, format));
  94. break;
  95. case 2:
  96. data.push(getDates(min, max, currentPickerDay, format));
  97. break;
  98. case 3:
  99. data.push(getHours(min, max, currentPickerDay, format));
  100. break;
  101. case 4:
  102. data.push(getMinutes(min, max, currentPickerDay, format));
  103. break;
  104. case 5:
  105. data.push(getSeconds(min, max, currentPickerDay, format));
  106. break;
  107. }
  108. }
  109. return data;
  110. }
  111. /**
  112. * 选中的值转换为时间类型,主要处理month从0开始的情况
  113. * @param value
  114. * @returns
  115. */
  116. export function getDateByValue(value) {
  117. //@ts-ignore
  118. return new (Date.bind.apply(Date, __spreadArray([void 0], value.map(function (v, i) { return String(i === 1 ? v - 1 : v); }), false)))();
  119. }
  120. /**
  121. * date转换为value数组
  122. * @param value
  123. * @returns
  124. */
  125. export function getValueByDate(date, precision) {
  126. var fields = [
  127. 'getFullYear',
  128. 'getMonth',
  129. 'getDate',
  130. 'getHours',
  131. 'getMinutes',
  132. 'getSeconds',
  133. ];
  134. var value = [];
  135. if (!date)
  136. return value;
  137. for (var i = 0; i < precisionLengthRecord[precision]; i++) {
  138. value.push(date[fields[i]]());
  139. if (i === 1) {
  140. value[1] = value[1] + 1;
  141. }
  142. }
  143. return value;
  144. }
  145. /**
  146. * 是否有效日期,主要处理月份对应可选日期,避免当前时间日期超出当月最后一天
  147. * @param value
  148. */
  149. export function getValidValue(value) {
  150. if (value.length > 2) {
  151. var maxDate = new Date(value[0], value[1], 0).getDate();
  152. if (value[2] > maxDate) {
  153. value = value.slice();
  154. value[2] = maxDate;
  155. }
  156. }
  157. return value;
  158. }
  159. /**
  160. * 比较两个date是否是同一时间
  161. * @param date1
  162. * @param date2
  163. */
  164. export function isEqualDate(date1, date2) {
  165. if (date1 instanceof Date && date2 instanceof Date) {
  166. return dayjs(date1).isSame(date2);
  167. }
  168. return date1 === date2;
  169. }