//每周对应的时间
getWeeklyPeriods(year) {
const yearWeekDates = [];
let currentDate = new Date(year, 0, 1); // January 1st of the specified year
let weekCounter = 1;
let currentMonth = 0;
while (currentDate.getFullYear() === year) {
console.log("------", currentDate.getDay())
const formattedDate = this.formatDate(currentDate);
if (!yearWeekDates[weekCounter]) {
yearWeekDates[weekCounter] = [];
}
if (currentMonth === currentDate.getMonth()) {
yearWeekDates[weekCounter].push(formattedDate);
if (currentDate.getDay() === 0) {
weekCounter++;
}
currentDate.setDate(currentDate.getDate() + 1);
}
if (currentMonth != currentDate.getMonth() && currentDate.getMonth() != 0) {
weekCounter++;
if (!yearWeekDates[weekCounter]) {
yearWeekDates[weekCounter] = [];
}
currentMonth = currentDate.getMonth()
}
}
return yearWeekDates;
},
// Function to format date as "yyyy mm dd"
formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
},
运行效果: