//获取当前月有几周 getWeeks(year, month) { var d = new Date(); // 该月第一天 d.setFullYear(year, month - 1, 1); var w1 = d.getDay(); if (w1 == 0) w1 = 7; // 该月天数 d.setFullYear(year, month, 0); var dd = d.getDate(); // 第一个周一 let d1; if (w1 != 1) d1 = 7 - w1 + 2; else d1 = 1; let week_count = Math.ceil((dd - d1 + 1) / 7); return week_count; }, //根据年月周获取该周从周一到周日的日期 getWeekTime(year, month, weekday, timeArr) { var d = new Date(); // 该月第一天 d.setFullYear(year, month - 1, 1); var w1 = d.getDay(); if (w1 == 0) w1 = 7; // 该月天数 d.setFullYear(year, month, 0); var dd = d.getDate(); // 第一个周一 let d1; if (w1 != 1) d1 = 7 - w1 + 2; else d1 = 1; var monday = d1 + (weekday - 1) * 7; var sunday = monday + 6; var from = year + "-" + month + "-" + monday; var to; if (sunday <= dd) { to = year + "-" + month + "-" + sunday; } else { d.setFullYear(year, month - 1, sunday); let days = d.getDate(); to = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + days; } if (timeArr) { return from + '~' + to } return '第' + weekday + '周:' + from + '~' + to } var weekNUm =_t.getWeeks(2022, 12); for (var k = 1; k <= weekNUm; k++) { _t.getWeekTime(todayYear, todayMonent, k); }
标签:月份,自然,month,获取,w1,year,var,setFullYear,d1 From: https://www.cnblogs.com/ht955/p/16976747.html