1 function getMonthLength(date) { 2 let d = new Date(date) 3 d.setMonth(d.getMonth()+1) 4 d.setDate('1') 5 d.setDate(d.getDate()-1) 6 return d.getDate() 7 } 8 9 var arr =[] 10 for (var i =1;i<=12;i++ ){ 11 var days = getMonthLength(`2021-${i}-01`) 12 for (var j =1;j<=days;j++ ){ 13 if(new Date(`2021-${i}-${j}`).getDay() === 0 || new Date(`2021-${i}-${j}`).getDay() === 6){ 14 console.log(`2021-${i}-${j}`) 15 arr.push(`2021-${i}-${j}`) 16 } 17 } 18 }
转载: https://www.javascriptc.com/interview-tips/zh_cn/javascript/array-multiple-intersections/