码迷,mamicode.com
首页 > 其他好文 > 详细

获取当月周六周日日期

时间:2020-07-09 15:07:43      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:提取   let   nbsp   ===   turn   return   计算   int   day   

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
        }

获取当月周六周日日期

标签:提取   let   nbsp   ===   turn   return   计算   int   day   

原文地址:https://www.cnblogs.com/mcsolo/p/13273466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!