一尘子、道法自然、博客园、前端

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)
posted @ 2024-09-23 11:43  一尘子!  阅读(802)  评论(0编辑  收藏  举报
Live2D
返回顶端