获取近一段时间的开始日期和结束日期

 获取近半年的开始日期和结束日期

 // 获取近半年
      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 lastMonthDay = new Date((year - 1), (12 - (6 - parseInt(month))), 0).getDate();
          //6个月前所在月的总天数小于现在的天日期    
          if(lastMonthDay < day){    
              dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + lastMonthDay;
          }else{
              dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + day;
          }
      }else{
        //6个月前所在月的总天数
          var lastMonthDay = new Date(year, (parseInt(month) - 6), 0).getDate(); 
          //6个月前所在月的总天数小于现在的天日期   
          if(lastMonthDay < day){    
             //当前天日期小于当前月总天数,2月份比较特殊的月份
              if(day < nowMonthDay){       
                  dateObj.last = year + '-' + (month - 6) + '-' + (lastMonthDay - (nowMonthDay - day));
              }else{
                  dateObj.last = year + '-' + (month - 6) + '-' + lastMonthDay;
              }
          }else{
              dateObj.last = year + '-' + (month - 6) + '-' + day;
          }
      }
      console.log(dateObj.last,dateObj.now);
    //设置到对应的表单内部
      this.searchData.dealTime=[dateObj.last,dateObj.now]  
    }

获取当前日期前一个月30天的日期

/* 获取过去时间,并传值给现在时间 */
    getPassMonthFormatDate() {
      var nowDate = new Date();
      var date = new Date(nowDate);
      date.setDate(date.getDate() - 30);
      var seperator1 = "-";
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var formatDate = year + seperator1 + month + seperator1 + strDate;
      this.getNowFormatDate(formatDate);
    },
    /* 获取现在时间,并接受过去时间的值 */
    getNowFormatDate(formatDate) {
      var date = new Date();
      var seperator1 = "-";
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var nowData = year + seperator1 + month + seperator1 + strDate;
       console.log(formatDate,nowData);
    //设置到对应的form表单内部
      this.searchData.dealTime=[formatDate,nowData]
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值