1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 获取当前周的周一的日期和周日的日期

获取当前周的周一的日期和周日的日期

时间:2022-06-23 01:53:26

相关推荐

获取当前周的周一的日期和周日的日期

// 计算本周的日期

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) 下周日

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。