utils.js 3.0 KB

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