1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| constructor(year, month) { this.year = year; this.month = month; this.firstDayOfMonth = moment([year, month, 1]); this.lastDayOfMonth = moment([year, month, 1]).endOf('month'); this.firstDayOfWeek = this.firstDayOfMonth.day(); }
const days = []; const totalCells = 42;
const day = this.firstDayOfMonth.clone().subtract(this.firstDayOfWeek - i, 'days'); days.push({ date: 2024-05-12 10:00:00 day: day.date(), month: day.month(), year: day.year(), isCurrentMonth: false, isToday: day.isSame(moment(), 'day'), isWeekend: day.day() === 0 || day.day() === 6 }); }
for (let i = 1; i <= daysInMonth; i++) { const day = moment([this.year, this.month, i]); days.push({ date: 2024-05-12 10:00:00 day: i, month: this.month, year: this.year, isCurrentMonth: true, isToday: day.isSame(moment(), 'day'), isWeekend: day.day() === 0 || day.day() === 6 }); }
for (let i = 1; i <= remainingCells; i++) { const day = this.lastDayOfMonth.clone().add(i, 'days'); days.push({ date: 2024-05-12 10:00:00 day: day.date(), month: day.month(), year: day.year(), isCurrentMonth: false, isToday: day.isSame(moment(), 'day'), isWeekend: day.day() === 0 || day.day() === 6 }); }
for (let i = 0; i < days.length; i += 7) { weeks.push(days.slice(i, i + 7)); }
return { month: this.month, year: this.year, weeks, firstDayOfMonth: this.firstDayOfMonth, lastDayOfMonth: this.lastDayOfMonth }; }
const businessDays = []; let current = this.firstDayOfMonth.clone();
while (current.isSameOrBefore(this.lastDayOfMonth, 'day')) { const dayOfWeek = current.day(); if (dayOfWeek !== 0 && dayOfWeek !== 6) { date: 2024-05-12 10:00:00 day: current.date(), isToday: current.isSame(moment(), 'day') }); } current.add(1, 'day'); }
return businessDays; }
const totalDays = this.lastDayOfMonth.date(); const businessDays = this.getBusinessDays().length; const weekendDays = totalDays - businessDays;
return { totalDays, businessDays, weekendDays, weeksCount: Math.ceil((this.firstDayOfWeek + totalDays) / 7) }; } }
console.log(calendar.getCalendarData()); console.log(calendar.getMonthlyStats());
|