因产品需要展示相关时间,现总结如下方法:以供日后参考:
获取每月有几周
// year:年 month:月 day:日
getWeeks(year, month, day) {
const d = new Date()
// 该月第一天
d.setFullYear(2018, 6, 1)
let w1 = d.getDay()
if (w1 === 0) {
w1 = 7
}
// 该月天数
d.setFullYear(2018, 7, 0)
const dd = d.getDate()
// 该月第一个周一
let d1
if (w1 !== 1) {
d1 = 7 - w1 + 2
} else {
d1 = 1
}
const WEEK_NUB = Math.ceil((dd - d1 + 1) / 7)
return WEEK_NUB
}获得周期名字
getWeekName() {
const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
const index = new Date().getDay()
const currDay = weekday[index]
return currDay
}获得当前日期在当月第几周
// a: 年 b: 月 c: 日 (不包括跟上个月重合的部分)
getMonthWeek(a, b, c) {
const date = new Date(a, parseInt(b) - 1, c)
const w = date.getDay()
const d = date.getDate()
return Math.ceil(
(d + 6 - w) / 7
)
}