Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 140x 4x 4x 4x 4x 4x 4x 4x 4x 121x 121x 121x 16x 16x 16x 121x 4x 4x 4x 4x 2x | // utils/calendarUtils.js
export function createDay(date = new Date(), options = {}) {
return {
id: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`, // Unique ID for each day
date,
weekOrder: options.weekOrder || 0,
central: options.central || false,
startOfWeek: options.startOfWeek || false,
selected: options.selected || false,
previousMonth: options.previousMonth || false,
currentMonth: options.currentMonth || false,
nextMonth: options.nextMonth || false,
past: options.past || false,
today: options.today || false,
future: options.future || false,
disabled: options.disabled || false,
tabindex: options.tabindex || '-1',
ariaCurrent: options.ariaCurrent || undefined,
classnames: options.classnames || '',
};
}
// createSingleMonth() is a function that creates a calendar for a given
// central date and returns an object with an array of months, each containing
// an array of weeks, each containing an array of days.
export function createSingleMonth(centralDate, { firstDayOfWeek = 0 } = {}) {
const months = [];
const startDate = new Date(centralDate.getFullYear(), centralDate.getMonth(), 1);
const endDate = new Date(centralDate.getFullYear(), centralDate.getMonth() + 1, 0);
const weeks = [];
let currentWeek = {
days: [],
id: `${centralDate.getFullYear()}-${centralDate.getMonth() + 1}-week-1`,
};
let weekCounter = 1;
// Reuse the same date object to avoid creating new Date instances
const date = new Date(startDate);
while (date <= endDate) {
const day = createDay(new Date(date), {
weekOrder: date.getDay(),
currentMonth: true,
id: `${centralDate.getFullYear()}-${centralDate.getMonth() + 1}-${date.getDate()}`,
});
currentWeek.days.push(day);
// If the day is Saturday, end the current week and start a new one
if (date.getDay() === 6) {
weeks.push(currentWeek);
weekCounter++;
currentWeek = {
days: [],
id: `${centralDate.getFullYear()}-${centralDate.getMonth() + 1}-week-${weekCounter}`,
};
}
// Move to the next day
date.setDate(date.getDate() + 1);
}
// Add the last week if it has any days
if (currentWeek.days.length > 0) {
E weeks.push(currentWeek);
}
// Add the month to the months array
months.push({
id: `${centralDate.getFullYear()}-${centralDate.getMonth() + 1}`, // Unique ID for each month
month: centralDate.toLocaleString('default', { month: 'long' }),
year: centralDate.getFullYear(),
weeks,
});
return { months };
}
/**
* isSameDay() is a function that checks if two dates are the same day.
* Dates are normalized to UTC to avoid issues with time zones.
* @param {Date} date1
* @param {Date} date2
* @returns {boolean}
*/
export const isSameDay = (date1, date2) => {
const utcDate1 = new Date(Date.UTC(date1.getUTCFullYear(), date1.getUTCMonth(), date1.getUTCDate()));
const utcDate2 = new Date(Date.UTC(date2.getUTCFullYear(), date2.getUTCMonth(), date2.getUTCDate()));
return utcDate1.getTime() === utcDate2.getTime();
};
|