dayjs 获取本周、上一周、下一周的第一天和最后一天的日期(周一&周日)
npm i dayjs --save
import dayjs from 'dayjs';
/**
* 获取日期的函数
*/
updateWeek(weekOffset) {
const today = dayjs()
const timestamp = Math.round(new Date())
const date = new Date(timestamp)
const weekday = date.getDay()
const targetWeek = weekday === 0 ? weekOffset - 1 : weekOffset
this.beginDate = today.add(targetWeek, 'week').startOf('week').add(1, 'day').format('YYYY-MM-DD')
this.endDate = today.add(targetWeek, 'week').endOf('week').add(1, 'day').format('YYYY-MM-DD')
}
/**
* 获取本周
*/
this.updateWeek(0)
/**
* 获取上周
*/
this.updateWeek(-1)
/**
* 获取下周
*/
this.updateWeek(1)
本文来自博客园,作者:一尘子!,转载请注明原文链接:https://www.cnblogs.com/mengqc1995/p/18426785