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、结果如下:

     

  • 相关阅读:
    CControlLayer
    CBiontCache
    CHero
    CWidgetMgr---cpp
    CWidgetMgr---H
    CXAnimation类
    CXAnimation.h动画类
    CXCommon.h工具类
    【leetcode】441. Arranging Coins
    【linux基础】关于ARM板子使用O3编译选项优化
  • 原文地址:https://www.cnblogs.com/moguzi12345/p/13820419.html
Copyright © 2011-2022 走看看