js 获取近一个月 近三个月近半年的时间范围

// 获取上个月的时间
    getLastMonth() {
      var nowdays = new Date();
      var year = nowdays.getFullYear();
      var month = nowdays.getMonth();
      if (month == 0) {
        month = 12;
        year = year - 1;
      }
      if (month < 10) {
        month = '0' + month;
      }
      var myDate = new Date(year, month, 0);
      var startDate = year + '-' + month + '-01'; //上个月第一天
      var endDate = year + '-' + month + '-' + myDate.getDate() ; //上个月最后一天
      this.setData({
        'options[0].value':startDate+'-'+endDate
      })
    },
    // 获取近三个月 往前推90天
    getLastthreeMonth(){
      var now = new Date();
      var year = now.getFullYear();
      //0-11表示1-12月
      var month = now.getMonth() + 1;
      var day = now.getDate();
      var dateObj = {};
      dateObj.now = year + '-' + month + '-' + day;
      //当前月的总天数
      var nowMonthDay = new Date(year, month, 0).getDate();
      //如果是1、2、3月,年数往前推一年    
      if(month - 3 <= 0){ 
          //3个月前所在月的总天数
          var last3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();
          //3个月前所在月的总天数小于现在的天日期    
          if(last3MonthDay < day){    
              dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + last3MonthDay;
          }else{
              dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;
          }
      }else{
        //3个月前所在月的总天数
          var last3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate(); 
          //3个月前所在月的总天数小于现在的天日期   
          if(last3MonthDay < day){    
             //当前天日期小于当前月总天数,2月份比较特殊的月份
              if(day < nowMonthDay){       
                  dateObj.last = year + '-' + (month - 3) + '-' + (last3MonthDay - (nowMonthDay - day));
              }else{
                  dateObj.last = year + '-' + (month - 3) + '-' + last3MonthDay;
              }
          }else{
              dateObj.last = year + '-' + (month - 3) + '-' + day;
          }
      }
      this.setData({
        'options[1].value':dateObj.last+'-'+dateObj.now
      })
    },
    // 获取近半年
    getLastHalfYear(){
      var now = new Date();
      var year = now.getFullYear();
      //0-11表示1-12月
      var month = now.getMonth() + 1;
      var day = now.getDate();
      var dateObj = {};
      dateObj.now = year + '-' + month + '-' + day;
      //当前月的总天数
      var nowMonthDay = new Date(year, month, 0).getDate();
      //如果是1、2、3,4,5,6月,年数往前推一年    
      if(month - 6 <= 0){ 
          //6个月前所在月的总天数
          var last3MonthDay = new Date((year - 1), (12 - (6 - parseInt(month))), 0).getDate();
          //6个月前所在月的总天数小于现在的天日期    
          if(last3MonthDay < day){    
              dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + last3MonthDay;
          }else{
              dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + day;
          }
      }else{
        //6个月前所在月的总天数
          var last3MonthDay = new Date(year, (parseInt(month) - 6), 0).getDate(); 
          //6个月前所在月的总天数小于现在的天日期   
          if(last3MonthDay < day){    
             //当前天日期小于当前月总天数,2月份比较特殊的月份
              if(day < nowMonthDay){       
                  dateObj.last = year + '-' + (month - 6) + '-' + (last3MonthDay - (nowMonthDay - day));
              }else{
                  dateObj.last = year + '-' + (month - 6) + '-' + last3MonthDay;
              }
          }else{
              dateObj.last = year + '-' + (month - 6) + '-' + day;
          }
      }
      this.setData({
        'options[2].value':dateObj.last+'-'+dateObj.now
      })
    }

 

posted @ 2020-10-30 15:40  cmwang2017  阅读(4488)  评论(0编辑  收藏  举报