utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. bem: () => bem,
  21. calcDateNum: () => calcDateNum,
  22. cloneDate: () => cloneDate,
  23. cloneDates: () => cloneDates,
  24. compareDay: () => compareDay,
  25. compareMonth: () => compareMonth,
  26. formatMonthTitle: () => formatMonthTitle,
  27. getDayByOffset: () => getDayByOffset,
  28. getNextDay: () => getNextDay,
  29. getPrevDay: () => getPrevDay,
  30. getToday: () => getToday,
  31. name: () => name,
  32. t: () => t
  33. });
  34. module.exports = __toCommonJS(stdin_exports);
  35. var import_utils = require("../utils");
  36. const [name, bem, t] = (0, import_utils.createNamespace)("calendar");
  37. const formatMonthTitle = (date) => t("monthTitle", date.getFullYear(), date.getMonth() + 1);
  38. function compareMonth(date1, date2) {
  39. const year1 = date1.getFullYear();
  40. const year2 = date2.getFullYear();
  41. if (year1 === year2) {
  42. const month1 = date1.getMonth();
  43. const month2 = date2.getMonth();
  44. return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
  45. }
  46. return year1 > year2 ? 1 : -1;
  47. }
  48. function compareDay(day1, day2) {
  49. const compareMonthResult = compareMonth(day1, day2);
  50. if (compareMonthResult === 0) {
  51. const date1 = day1.getDate();
  52. const date2 = day2.getDate();
  53. return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
  54. }
  55. return compareMonthResult;
  56. }
  57. const cloneDate = (date) => new Date(date);
  58. const cloneDates = (dates) => Array.isArray(dates) ? dates.map(cloneDate) : cloneDate(dates);
  59. function getDayByOffset(date, offset) {
  60. const cloned = cloneDate(date);
  61. cloned.setDate(cloned.getDate() + offset);
  62. return cloned;
  63. }
  64. const getPrevDay = (date) => getDayByOffset(date, -1);
  65. const getNextDay = (date) => getDayByOffset(date, 1);
  66. const getToday = () => {
  67. const today = new Date();
  68. today.setHours(0, 0, 0, 0);
  69. return today;
  70. };
  71. function calcDateNum(date) {
  72. const day1 = date[0].getTime();
  73. const day2 = date[1].getTime();
  74. return (day2 - day1) / (1e3 * 60 * 60 * 24) + 1;
  75. }