获取当月周六周日日期
Posted mcsolo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取当月周六周日日期相关的知识,希望对你有一定的参考价值。
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
}
以上是关于获取当月周六周日日期的主要内容,如果未能解决你的问题,请参考以下文章