utils.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.formatMonthTitle = formatMonthTitle;
  6. exports.compareMonth = compareMonth;
  7. exports.compareDay = compareDay;
  8. exports.getDayByOffset = getDayByOffset;
  9. exports.getPrevDay = getPrevDay;
  10. exports.getNextDay = getNextDay;
  11. exports.calcDateNum = calcDateNum;
  12. exports.copyDates = copyDates;
  13. exports.getMonthEndDay = getMonthEndDay;
  14. exports.getMonths = getMonths;
  15. exports.ROW_HEIGHT = void 0;
  16. var ROW_HEIGHT = 64;
  17. exports.ROW_HEIGHT = ROW_HEIGHT;
  18. function formatMonthTitle(date) {
  19. if (!(date instanceof Date)) {
  20. date = new Date(date);
  21. }
  22. return "".concat(date.getFullYear(), "\u5E74").concat(date.getMonth() + 1, "\u6708");
  23. }
  24. function compareMonth(date1, date2) {
  25. if (!(date1 instanceof Date)) {
  26. date1 = new Date(date1);
  27. }
  28. if (!(date2 instanceof Date)) {
  29. date2 = new Date(date2);
  30. }
  31. var year1 = date1.getFullYear();
  32. var year2 = date2.getFullYear();
  33. var month1 = date1.getMonth();
  34. var month2 = date2.getMonth();
  35. if (year1 === year2) {
  36. return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
  37. }
  38. return year1 > year2 ? 1 : -1;
  39. }
  40. function compareDay(day1, day2) {
  41. if (!(day1 instanceof Date)) {
  42. day1 = new Date(day1);
  43. }
  44. if (!(day2 instanceof Date)) {
  45. day2 = new Date(day2);
  46. }
  47. var compareMonthResult = compareMonth(day1, day2);
  48. if (compareMonthResult === 0) {
  49. var date1 = day1.getDate();
  50. var date2 = day2.getDate();
  51. return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
  52. }
  53. return compareMonthResult;
  54. }
  55. function getDayByOffset(date, offset) {
  56. date = new Date(date);
  57. date.setDate(date.getDate() + offset);
  58. return date;
  59. }
  60. function getPrevDay(date) {
  61. return getDayByOffset(date, -1);
  62. }
  63. function getNextDay(date) {
  64. return getDayByOffset(date, 1);
  65. }
  66. function calcDateNum(date) {
  67. var day1 = new Date(date[0]).getTime();
  68. var day2 = new Date(date[1]).getTime();
  69. return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
  70. }
  71. function copyDates(dates) {
  72. if (Array.isArray(dates)) {
  73. return dates.map(function (date) {
  74. if (date === null) {
  75. return date;
  76. }
  77. return new Date(date);
  78. });
  79. }
  80. return new Date(dates);
  81. }
  82. function getMonthEndDay(year, month) {
  83. return 32 - new Date(year, month - 1, 32).getDate();
  84. }
  85. function getMonths(minDate, maxDate) {
  86. var months = [];
  87. var cursor = new Date(minDate);
  88. cursor.setDate(1);
  89. do {
  90. months.push(cursor.getTime());
  91. cursor.setMonth(cursor.getMonth() + 1);
  92. } while (compareMonth(cursor, maxDate) !== 1);
  93. return months;
  94. }