//获取某年某月每周的开始时间和结束时间
function getInfo(year, month) {
let d = new Date();//
// what day is first day
d.setFullYear(year, month-1, 1);
let w1 = d.getDay();//某月的第一天是星期几
console.log("某月的第一天是星期:" + w1);
if (w1 == 0){
w1 = 7;
}
// total day of month
d.setFullYear(year, month, 0);
let dd = d.getDate();//某月的总天数
console.log("某月的总天数:" + dd);
let d1
// first Monday
if (w1 != 1){
d1 = 7 - w1 + 2;
}
else {
d1 = 1;
}
let week_count = Math.ceil((dd-d1+1)/7);
//let week_count = Math.ceil(dd/7);
console.log(year + "年" + month + "月有" + week_count +"周");
for (let i = 0; i < week_count; i++) {
// let monday = d1+i*7;
let monday = d1+i*7;
let sunday = monday + 6;
let startTime = year+"/"+month+"/"+monday;
let endTime;
if (sunday <= dd) {
endTime = year+"/"+month+"/"+sunday;
}
else {
d.setFullYear(year, month-1, sunday);
endTime = d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate();
}
console.log("第"+(i+1)+"周 从" + startTime + " 到 " + endTime + "");
}
}
getInfo(2020,4)//2020年4月
说明:这个可以根据需要自行调整。