获取当前月的周六或者周日
代码如下:
// 获取一个月有多少天
function getMonthLength(date) {
let d = new Date(date)
// 获取月份的下个月
d.setMonth(d.getMonth() + 1)
// 设置为下个月第一天
d.setDate('1')
// 设置时间为下月的前一天
d.setDate(d.getDate() - 1)
// 返回天数
return d.getDate()
}
// 获取本月的周六日 type 0-周日 6-周六
function getWeekDay(year,month,type) {
var arr =[]
var days = getMonthLength(`${year}-${month}`)
for (var j = 1; j <= days; j++) {
if (new Date(`${year}-${month}-${j}`).getDay() === type) {
arr.push(`${year}-${month}-${j}`)
}
}
return arr
}
let result = getWeekDay('2021','11', 0)
console.log(result)