JS生成任意时间段的月份

136 阅读1分钟
  // 生成某个时间段内所有的月份 '2021-06' '2022-05'
   function getMonthBetween (start, end) {
      const result = []
      // 切割起始年月
      const s = start.split('-')
      // 切割结束年月
      const e = end.split('-')
      // 获取时间对象
      const min = new Date()
      const max = new Date()
      // 设置起始时间 第三个参数需要设置成1,否则跨年出现少第一个月的bug
      min.setFullYear(s[0], s[1], 1)
      // 设置结束时间
      max.setFullYear(e[0], e[1], 1)
      // 复制一份起始时间对象
      const curr = min
      // 定义字符串
      let str = ''
      // 起始时间在结束时间之前
      // eslint-disable-next-line
      while (curr <= max) {
        // 获取此时间的月份
        var month = curr.getMonth()
        // 如果月份为0,也就是代表12月份
        if (month === 0) {
          str = (curr.getFullYear() - 1) + '-' + 12
        } else { // 正常月份
          str = curr.getFullYear() + '-' + month
        }
        // 将此年月加入数组
        result.push({
          date: month === 0 ? 12 : month,
          full_date: str,
          name: `${month === 0 ? 12 : month}月`
        })
        // 更新此时间月份 第二个参数需要设置成1,否则跨年出现少第一个月的bug
        curr.setMonth(month + 1, 1)
      }
      return result
    }