utils.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.formatMonthTitle = formatMonthTitle;
  4. exports.compareMonth = compareMonth;
  5. exports.compareDay = compareDay;
  6. exports.getDayByOffset = getDayByOffset;
  7. exports.getPrevDay = getPrevDay;
  8. exports.getNextDay = getNextDay;
  9. exports.calcDateNum = calcDateNum;
  10. exports.copyDate = copyDate;
  11. exports.copyDates = copyDates;
  12. exports.t = exports.bem = exports.createComponent = void 0;
  13. var _utils = require("../utils");
  14. var _createNamespace = (0, _utils.createNamespace)('calendar'),
  15. createComponent = _createNamespace[0],
  16. bem = _createNamespace[1],
  17. t = _createNamespace[2];
  18. exports.t = t;
  19. exports.bem = bem;
  20. exports.createComponent = createComponent;
  21. function formatMonthTitle(date) {
  22. return t('monthTitle', date.getFullYear(), date.getMonth() + 1);
  23. }
  24. function compareMonth(date1, date2) {
  25. var year1 = date1.getFullYear();
  26. var year2 = date2.getFullYear();
  27. var month1 = date1.getMonth();
  28. var month2 = date2.getMonth();
  29. if (year1 === year2) {
  30. return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
  31. }
  32. return year1 > year2 ? 1 : -1;
  33. }
  34. function compareDay(day1, day2) {
  35. var compareMonthResult = compareMonth(day1, day2);
  36. if (compareMonthResult === 0) {
  37. var date1 = day1.getDate();
  38. var date2 = day2.getDate();
  39. return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
  40. }
  41. return compareMonthResult;
  42. }
  43. function getDayByOffset(date, offset) {
  44. date = new Date(date);
  45. date.setDate(date.getDate() + offset);
  46. return date;
  47. }
  48. function getPrevDay(date) {
  49. return getDayByOffset(date, -1);
  50. }
  51. function getNextDay(date) {
  52. return getDayByOffset(date, 1);
  53. }
  54. function calcDateNum(date) {
  55. var day1 = date[0].getTime();
  56. var day2 = date[1].getTime();
  57. return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
  58. }
  59. function copyDate(dates) {
  60. return new Date(dates);
  61. }
  62. function copyDates(dates) {
  63. if (Array.isArray(dates)) {
  64. return dates.map(function (date) {
  65. if (date === null) {
  66. return date;
  67. }
  68. return copyDate(date);
  69. });
  70. }
  71. return copyDate(dates);
  72. }