生成数据的格式:

生成数据的函数:
点击查看代码
// 生成日历
generateCalendar(startDate, checkedDates) {
// 获取当前日期
const today = new Date();
today.setHours(0, 0, 0, 0);
// 获取起始日期
const baseDate = new Date(startDate);
baseDate.setHours(0, 0, 0, 0);
// checkedDates 是一个日期数组,默认空数组
checkedDates = checkedDates ? checkedDates.map(date => new Date(date).setHours(0, 0, 0, 0)) : [];
// 定义一个空数组,用于存储日历
const calendar = [];
// 循环生成日历
for (let i = 0; i < 7; i++) {
// 获取当前日期
const currentDate = new Date(baseDate);
currentDate.setDate(baseDate.getDate() + i);
currentDate.setHours(0, 0, 0, 0);
// 获取当前日期的月份和日期
const month = currentDate.getMonth() + 1;
const day = currentDate.getDate();
// 判断当前日期是否在 checkedDates 数组中
calendar.push({
date: `${month}.${day}`,
checked: checkedDates.some(date => date === currentDate.getTime()), // 检查是否包含在 checkedDates 中
isToday: currentDate.getTime() === today.getTime()
});
}
// 返回日历
return calendar;
}
使用方式:
const today = new Date(); // 获取当前日期
today.setDate(today.getDate() - 1); // 设置为当前日期的前一天
const startDate = today; // 将 startDate 设置为当前日期的前一天
const checkedDates = [
new Date(2025, 3, 10).getTime(), // 2025年4月10日
new Date(2025, 3, 9).getTime() // 2025年4月9日
];
调用:generateCalendar(startDate, checkedDates)// startDate:开始时间,选中的日期时间(数组时间戳格式)
浙公网安备 33010602011771号