生成按月递增循环日期

工具方法: 
// 日期加法
    DateAdd(strInterval, Number) {   
        let dtTmp = this;  
        switch (strInterval) {   
          case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));  
          break;
          case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));  
          break;
          case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));  
          break;
          case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));  
          break;
          case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));  
          break;
          case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());  
          break;
          case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());  
          break;
          case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());  
          break;
          default: return dtTmp;
        }  

      }; 
      // 判断某一天是否为该月最后一天
      isMaxDay(){
        let isMaxDay = false;
        let time = new Date(this);
        let month = time.getMonth();
        let nextDay = time.DateAdd('d',1);
        let nextDayMonth = nextDay.getMonth();
        if(nextDayMonth - month!=0) {
          isMaxDay = true;
        }
        return isMaxDay;

      };
      // 日期减法
      DateSubtract(strInterval, Number) {   
        let dtTmp = this;  
        switch (strInterval) {   
          case 's' :return new Date(Date.parse(dtTmp) - (1000 * Number));  
          break;
          case 'n' :return new Date(Date.parse(dtTmp) - (60000 * Number));  
          break;
          case 'h' :return new Date(Date.parse(dtTmp) - (3600000 * Number));  
          break;
          case 'd' :return new Date(Date.parse(dtTmp) - (86400000 * Number));  
          break;
          case 'w' :return new Date(Date.parse(dtTmp) - ((86400000 * 7) * Number));  
          break;
          default: return dtTmp;
        }  
      };
      
      
      
逻辑方法:       
        pusharr(times,date){
           this.state.arr.push(date);
           // 判断该天是否为改月最后一天,如果是走第一种算法
           if(date.isMaxDay()){
            let nextTime = '';
            for (let i = 1; i < times; i++) {
              nextTime = date.DateAdd('d',31);
              let basicM = date.getMonth();
              let nextM = nextTime.getMonth();
              if(nextM - basicM ===2) {
               let nextD = nextTime.getDate();
               nextTime  = nextTime.DateSubtract('d',nextD);
             }
             date = nextTime;
             this.state.arr.push(nextTime);
             console.log(this.state.arr);
           }
         }
           // 该天不是该月最后一天,走第二种算法
           else {
             let nextTime = '';
             for (let i = 1; i<times;i++) {
              nextTime = date.DateAdd('m',i);
              let basicM = date.getMonth();
              let nextM = nextTime.getMonth();
              if (nextM - basicM ===i+1) {
                let nextD = nextTime.getDate();
                nextTime  = nextTime.DateSubtract('d',nextD);
              } else if (nextM - basicM <=0) {
               times = times -i;
               this.pusharr(times,nextTime);
               break;
             }
             this.state.arr.push(nextTime);
             console.log(this.state.arr);
           }
         }
       },

 

posted @ 2017-06-21 17:39  沐子馨  阅读(684)  评论(0编辑  收藏  举报