// 计算本周的日期
getDate (n) {
let now = new Date()
let year = now.getFullYear()
// 因为月份是从0开始的,所以获取这个月的月份数要加1才行
let month = now.getMonth() + 1
let date = now.getDate()
let day = now.getDay()
// 判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
if (day !== 0) {
n = n + (day - 1)
} else {
n = n + day
}
if (day) {
// 这个判断是为了解决跨年的问题
if (month > 1) {
// month = month
} else {
year = year - 1
month = 12
}
}
now.setDate(now.getDate() - n)
year = now.getFullYear()
month = now.getMonth() + 1
date = now.getDate()
let s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date)
return s
}
this.getDate(0) 周一的日期
this.getDate(-6) 周日的日期
this.getDate(-7) 下周一
thi.getDate(-13) 下周日