js根据日期获取所在周

一、获取时间所在周的周一、周五
function getFirstLastDay (time) {
          
       let date = new Date(time)
            let Time = date.getTime()
            let day = date.getDay()
            let oneDayTime =24*60*60*1000
  
            //周一
            let MondayTime = Time - (day - 1) * oneDayTime
            //周日
            let SundayTime = Time + (7 - day) * oneDayTime
 
            let monday =new Date(MondayTime)
            let sunday = new Date(SundayTime)
       
       return { monday, sunday } 
}

 

二、获取日期所在周的周一
function getFirstDay (date) {
    let day =date.getDay() || 7  

  return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1 - day) }

 

三、获取日期所在周的周五
function getLastDayOf (date) {
    let day = date.getDay() || 7

  return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 7 - day) }

 

posted @ 2021-03-12 15:21  YINGYAN  阅读(3372)  评论(0编辑  收藏  举报