获取当月周六周日日期

getWeeks() {
            const date = new Date()
            const currentYear = date.getFullYear()
            let currentMonth = date.getMonth() + 1
            if (currentMonth < 10) currentMonth = '0' + currentMonth
            const firstDate = new Date(`${currentYear}-${currentMonth}-01`)
            const day = parseInt(firstDate.getDay() === 0 ? 7 : firstDate.getDay())
            const days = new Date(currentYear, currentMonth, 0).getDate()

            // 计算当月周数
            let weeks
            const temp = days % 7
            if (7 - day >= temp) {
                weeks = parseInt(days / 7) + 1
            } else {
                weeks = parseInt(days / 7) + 2
            }
            const lastDay = new Date(`${currentYear}-${currentMonth}-${days}`).getDay()
            if (lastDay === 0) {
                weeks--
            }

            // 提取本月的周六和周末
            let freeDays = []
            if (day <= 6) {
                freeDays.push(7 - day)
                freeDays.push(1 + (7 - day))
            }
            for (let i = 1, j = weeks - 1; i < j; i++) {
                const last = freeDays[freeDays.length - 1]
                freeDays.push(last + 6)
                freeDays.push(last + 7)
            }
            if (lastDay === 0) {
                const last = freeDays[freeDays.length - 1]
                freeDays.push(last + 6)
                freeDays.push(last + 7)
            }

            let result = []
            freeDays.forEach(m => {
                result.push(`${currentYear}-${currentMonth}-${m < 10 ? ('0' + m) : m}`)
            })
            return result
        }

你可能感兴趣的:(获取当月周六周日日期)