js 日期加天数的方法,获取周一、周六 加6天 获取周日、周五

在这里插入图片描述

        <el-date-picker
          v-model="week_value"
          type="week"
          format="yyyy 第 WW 周"
          value-format="yyyy-MM-dd"
          placeholder="选择周"
          :picker-options="{ firstDayOfWeek: 6 }"  //6 设周六为每周的开始 ,0、1、2、3、4、5、6[代表周日、周一~周六]
          style="width: 150px"
          @change="getData">
        </el-date-picker>
 week_value: '',

// 标准日转化为年月日
      transformTime(date){
        var dd = new Date(date);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;
        var d = dd.getDate();
        m = m < 9 ? "0" + m : m;
        d = d < 9 ? "0" + d : d;
        return  y + "-" + m + "-" + d;
      },

      //字符串转日期格式,strDate要转为日期格式的字符串
      setDate(strDate){
        var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
        function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
        return date;
      } ,

      //获取指定日期所在周的周一的日期
      GetMonday(dd) {
        var week = dd.getDay(); //获取时间的星期数
        var minus = week ? week - 1 : 6;
        dd.setDate(dd.getDate() - minus); //获取minus天前的日期
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1; //获取月份
        var d = dd.getDate();
        return y + "-" + m + "-" + d;
      },

      //获取指定日期所在周的上周六的日期
      GetSaturday(dd) {
        //获取时间段
        var date = new Date(dd);

        //获取日期字符串对应的时间戳
        var timestamp = new Date(date).getTime();

        //获取星期几
        var currentDay = new Date(date).getDay();

        //获取时间戳
        var previousSaturdayTime;
        // var nextFridayTime;

        if (currentDay == 0) {
          //获取该天前一个星期六的时间戳
          previousSaturdayTime = timestamp- 24 * 60 * 60 * 1000;
          //获取该天后一个星期五的时间戳
          // nextFridayTime = timestamp + (5 - currentDay) * 24 * 60 * 60 * 1000;
        } else {
          //获取该天前一个星期六的时间戳
          //previousSaturdayTime = timestamp - (currentDay + 1) * 24 * 60 * 60 * 1000;
          previousSaturdayTime = timestamp - (currentDay + 1) * 24 * 60 * 60 * 1000;
          //获取该天后一个星期五的时间戳
          // nextFridayTime = timestamp + (5 - currentDay) * 24 * 60 * 60 * 1000;
        }
        var saturdayTime = new Date(previousSaturdayTime);
        //nextFridayTime = new Date(nextFridayTime);

        let saturdayYear = saturdayTime.getFullYear();
        let saturdayMonth = saturdayTime.getMonth() + 1;
        let saturdayDay = saturdayTime.getDate();
        saturdayMonth = saturdayMonth >= 10 ? saturdayMonth : '0' + saturdayMonth;
        saturdayDay = saturdayDay >= 10 ? saturdayDay : '0' + saturdayDay;

        let lastSatDay = saturdayYear+'-'+saturdayMonth+'-'+saturdayDay;
        return lastSatDay
      },


      getData(){
        var that = this
        var date = that.week_value;
        var  dd = that.setDate(date);
        var date_begin = that.GetSaturday(dd);
        var d_end = that.setDate(date_begin);
        d_end = new Date((d_end/1000+(86400*6))*1000);  //日期加6天
        var date_end = that.transformTime(d_end);
    }
//日期加天数的方法
//dataStr日期字符串
//dayCount 要增加的天数
//return 增加n天后的日期字符串
function dateAddDays(dataStr,dayCount) {
	var strdate=dataStr; //日期字符串
	var isdate = new Date(strdate.replace(/-/g,"/"));  //把日期字符串转换成日期格式
	isdate = new Date((isdate/1000+(86400*dayCount))*1000);  //日期加1天
	var pdate = isdate.getFullYear()+"-"+(isdate.getMonth()+1)+"-"+(isdate.getDate());   //把日期格式转换成字符串
 
	return pdate;
}

你可能感兴趣的:(javascript,开发语言,ecmascript)