zoukankan      html  css  js  c++  java
  • 获取两个日期之间的年月(可跨年)

    1、例如:输入起止时间,获取以下内容

     2、方法如下:

    getYearAndMonth(start, end) {
          let month;
          const result = [];
          const newResult = [];
          const starts = start.split('-');
          const ends = end.split('-');
          let staYear = parseInt(starts[0]);
          let staMon = parseInt(starts[1]) - 1;
          const endYear = parseInt(ends[0]);
          const endMon = parseInt(ends[1]);
          while (staYear <= endYear) {
            if (staYear === endYear) {
              while (staMon < endMon) {
                staMon++;
                result.push({year: staYear, month: staMon});
              }
              staYear++;
            } else {
              staMon++;
              if (staMon > 12) {
                staMon = 1;
                staYear++;
              }
              result.push({year: staYear, month: staMon});
            }
          }
          for(let i=0; i<result.length; i++){
            const year = result[i].year;
            const monthinit = result[i].month;
            if(monthinit<10){
              month = '0' + monthinit;
            }else{
              month = monthinit + '';
            }
            const ym = year + '-' + month;
            newResult.push(ym);
          }
          console.log(result);
          console.log(newResult);
          return newResult;  //return 内容可以根据实际返回 
    }

    3、结果如下:

     

  • 相关阅读:
    IOC和DI的区别
    hdu 1217(Floyed)
    hdu 2112(字典树+最短路)
    hdu 4081(次小生成树)
    hdu 1811(缩点+拓扑排序+并查集)
    poj 3026(BFS+最小生成树)
    hdu 3635(并查集)
    hdu 3047(扩展并查集)
    hdu 1116(并查集+欧拉路径)
    poj 1679(次小生成树)
  • 原文地址:https://www.cnblogs.com/moguzi12345/p/13820419.html
Copyright © 2011-2022 走看看