获取当前时间前6个月和后6个月,封装成数组

GetPreMonthDay: function () {
      //创建现在的时间
      let data = new Date();
      //获取年
      let year = data.getFullYear();
      let years = data.getFullYear();
      //获取月
      let mon = data.getMonth() + 1;
      let mons = data.getMonth() + 1;
      let arry = [];
      let list = [];
      let mon1 = mon, mon2 = mons,year1=year,year2=years;
      //获取前6个月的数组
      for (let i = 0; i < 7; i++) {
        mon1 = i == 0 ? parseInt(mon1) - 6 : parseInt(mon1) + 1;
        if (parseInt(mon1) <= 0) {
          year1 = parseInt(year1) - 1;
          mon1 = parseInt(mon1) + 12;
        } else if (parseInt(mon1) > 12) {
          year1 = parseInt(year1) + 1;
          mon1 = parseInt(mon1) - 12;
        }
        if (parseInt(mon1) < 10) {
          mon1 = "0" + mon1;
        }
        arry[i] = {
          name: year1 + "年" + mon1 + '月',
          value: year1 + '-' + mon1,
        };
      }
      //获取后6个月的数组
      for (let i = 0; i < 6; i++) {
        mon2 = parseInt(mon2) + 1;
        if (parseInt(mon2) > 12) {
          year2 = parseInt(year2) + 1;
          mon2 = parseInt(mon2) - 12;
        }
        if (parseInt(mon2) < 10) {
          mon2 = "0" + mon2;
        }
        list[i] = {
          name: year2 + "年" + mon2 + '月',
          value: year2 + '-' + mon2,
        };
      }
      let time = arry.concat(list);
      return time;
    },

 

posted @ 2018-12-14 10:48  秀&莹&锐  阅读(504)  评论(0编辑  收藏  举报