function getWeekStr(str) { // 将字符串转为标准时间格式 str2 = Date.parse(str); let date = new Date(str2); let month = date.getMonth() + 1; let week = getWeekFromDate(date); if (week === 0) {//第0周归于上月的最后一周 month = date.getMonth(); let dateLast = new Date(); let dayLast = new Date(dateLast.getFullYear(), dateLast.getMonth(), 0).getDate(); let timestamp = new Date(new Date().getFullYear(), new Date().getMonth() - 1, dayLast); week = getWeekFromDate(new Date(timestamp)); } let time = month + "月第" + week + "周"; return time; } function getWeekFromDate(date) { // 将字符串转为标准时间格式 let w = date.getDay();//周几 if (w === 0) { w = 7; } let week = Math.ceil((date.getDate() + 6 - w) / 7) - 1; return week; }
console.log("2022-12-28---" + getWeekStr("2022-12-28"));
标签:week,几周,JS,date,let,new,Date,选中,getMonth From: https://www.cnblogs.com/liuqingxia/p/17010491.html