获取当前月总共有多少周 生成数组
// 获取当前日期
const now = new Date();
// 获取当前年份和月份
const currentYear = now.getFullYear(); // 如果要做切换年月份 这里传 动态值 2023
const currentMonth = now.getMonth() + 1; // 8
// 计算当前月份有多少周
const firstDay = new Date(currentYear, currentMonth - 1, 1);
const lastDay = new Date(currentYear, currentMonth, 0);
const totalWeeks = Math.ceil((lastDay.getDate() - firstDay.getDate() + (firstDay.getDay() === 0 ? 1 : 0)) / 7);
// 生成数组
const weeksArray = Array.from({ length: totalWeeks }, (_, index) => `第${index + 1}周`);
console.log(weeksArray);// ['第1周', '第2周', '第3周', '第4周', '第5周']