zoukankan      html  css  js  c++  java
  • 生成按月递增循环日期

    工具方法: 
    // 日期加法
        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);
               }
             }
           },
  • 相关阅读:
    java如何将char类型的数字转换成int型的数字,而不是Ascii
    java 二分查找的注意事项
    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
    java中Math的常用方法整理
    判断字符串是否可由重复子字符串组成
    P3558 [POI2013]BAJ-Bytecomputer
    BZOJ 3329. Xorequ
    Codeforces 1221F. Choose a Square
    Codeforces 1221E. Game With String
    Codeforces 1221D. Make The Fence Great Again
  • 原文地址:https://www.cnblogs.com/momo798/p/7060861.html
Copyright © 2011-2022 走看看