js获取日期范围内日、周、月

1、获取日期范围内天数

返回:[2022-03-26, 2022-03-27, 2022-03-28]

    // 获取日
    // startDate 2022-03-26
    // endDate 2022-04-26
    getDay(startDate, endDate) {
      var result = new Array();
      var ab = startDate.split("-")
      var ae = endDate.split("-")
      var db = new Date()
      db.setUTCFullYear(ab[0], ab[1]-1, ab[2])
      var de = new Date()
      de.setUTCFullYear(ae[0], ae[1]-1, ae[2])
      var unixDb=db.getTime()
      var unixDe=de.getTime()
      for(var k=unixDb;k<=unixDe;){
        result.push(this.formatDate(new Date(parseInt(k)), 'yyyy-MM-dd'))
        k=k+24*60*60*1000
      }
      return result
    },
    // 日期格式化
    formatDate  (date, fmt) {
      if (date === "" || date === null || date === undefined) {
        return null;
      }
      if (fmt === "" || fmt === null || fmt === undefined) {
        fmt = "yyyy-MM";
      }
      date = new Date(date);
      var o = {
        "M+": date.getMonth() + 1, // 月份
        "d+": date.getDate(), // 日
        "h+": date.getHours(), // 小时
        "m+": date.getMinutes(), // 分
        "s+": date.getSeconds(), // 秒
        "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
        S: date.getMilliseconds(), // 毫秒
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(
              RegExp.$1,
              RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
          );
      }
      return fmt;
    },

2、获取日期范围内周数

注意:该方式是以每年52周计算
返回:[202152, 202201, 202202, 202203]

    // 获取周
    // startDate 2022-03-26
    // endDate 2022-04-26
    getWeak(start, end){
      // 1、获取开始日期属于第几周,哪年
      let startWeekNum = this.getWeekNum(start)
      let startYearNum = parseInt(start.split('-')[0])

      if (startWeekNum >= 53){
        startWeekNum = 52
      }

      // 2、获取结束日期属于第几周,哪年
      let endWeekNum = this.getWeekNum(end)
      let endYearNum = parseInt(end.split('-')[0])

      if (endWeekNum >= 53){
        endWeekNum = 52
      }

      // 3、计算周数
      let result = []
      let yearAdd = 0 // 跨年数
      let yearWeekNum = startWeekNum // 当前取到的周数
      for (let i=0; i<=((endYearNum - startYearNum) * 52 - startWeekNum + endWeekNum); i++){
        if ((startWeekNum + i) <= 0) { // 如果开始周数是0,则取开始年上一年的52周
          result.push((startYearNum-1) + "52")
        }else {
          if (yearWeekNum === 52) {
            yearAdd++
          }
          yearWeekNum = startWeekNum + i - yearAdd * 52
          if (yearWeekNum <= 0) { // 如果开始周数是0,则取开始年上一年的52周
            result.push(startYearNum + "52")
          }else {
            if (yearWeekNum < 10){
              result.push((startYearNum + yearAdd) + "0" + yearWeekNum)
            }else {
              result.push((startYearNum + yearAdd) + "" + yearWeekNum)
            }
          }
        }
      }
      return result
    },
    getWeekNum(dt) {
        let d1 = new Date(dt);
        let d2 = new Date(dt);
        d2.setMonth(0);
        d2.setDate(1);
        let rq = d1 - d2;
        let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
        let num = Math.ceil(days / 7);
        return num;
    }

3、获取日期范围内月数

返回:[2022-01, 2022-02, 2022-03, 2022-04]

// 获取月
    // startDate 2022-01-26
    // endDate 2022-04-26
    getMonth(start, end){
      var result = []
      var s = start.split('-')
      var e = end.split('-')
      var min = new Date()
      var max = new Date()
      min.setFullYear(s[0], s[1] * 1 - 1, 1) //开始日期

      max.setFullYear(e[0], e[1] * 1 - 1, 1) //结束日期
      var curr = min
      while (curr <= max) {
        var month = curr.getMonth()
        result.push(this.formatDate(curr, 'yyyy-MM'))
        curr.setMonth(month + 1)
      }
      return result
    },

你可能感兴趣的:(javascript,vue)