JS - 自定义一周的开始和结束,计算日期所在月的周数、所在月第几周、所在周的日期范围

自定义一周的开始和结束,计算日期所在月的周数、所在月第几周、所在周的日期范围

一. 方法使用

根据月开始日期星期几、月结束日期星期几,计算始周、末周占月的天数(每周周期段:上周六 —— 本周五)

getStrEndDay(str_week, end_week) {
 // 始周占月的天数:开始周几(str_week) 距离 结束(周五) 有几天
 let str_num = 0;
 switch (str_week) {
   case 1:
     str_num = 5;
     break;
   case 2:
     str_num = 4;
     break;
   case 3:
     str_num = 3;
     break;
   case 4:
     str_num = 2;
     break;
   case 5:
     str_num = 1;
     break;
   case 6:
     str_num = 7;
     break;
   case 7:
     str_num = 6;
     break;
 }
 // 末周占月的天数:开始(周六) 距离 结束周几(end_week) 有几天
 let end_num = 0;
 switch (end_week) {
   case 1:
     end_num = 3;
     break;
   case 2:
     end_num = 4;
     break;
   case 3:
     end_num = 5;
     break;
   case 4:
     end_num = 6;
     break;
   case 5:
     end_num = 7;
     break;
   case 6:
     end_num = 1;
     break;
   case 0:
     end_num = 2;
     break;
 }
 return { str_num, end_num };
}

根据年月,获取每月有几周

getWeekCount(year, month) {
  // 获取天数
  var day_num = new Date(year, month, 0).getDate();
  // 该月第一天、最后一天
  const str_day = new Date().setFullYear(year, month - 1, 1);
  const end_day = new Date().setFullYear(year, month, 0);
  //该月第一周、最后一周
  const str_week = new Date(str_day).getDay();
  const end_week = new Date(end_day).getDay();
  // 该月减去第一周、最后一周的天数
  const numRes = this.getStrEndDay(str_week, end_week);
  const oth_day = day_num - numRes.end_num - numRes.str_num;
  // 总周数
  let week_num = oth_day / 7;
  if (numRes.str_num == 7) {
    week_num += 1;
  }
  if (numRes.end_num != 0) {
    week_num += 1;
  }
  return week_num;
}

根据年月日获取当前周

getCurWeek(year, month, day) {
  // 该月第一天、最后一天
  const str_day = new Date().setFullYear(year, month - 1, 1);
  const end_day = new Date().setFullYear(year, month, 0);
  //该月第一周、最后一周
  const str_week = new Date(str_day).getDay();
  const end_week = new Date(end_day).getDay();
  // 该月减去第一周的天数
  const numRes = this.getStrEndDay(str_week, end_week);
  const oth_day = day - numRes.str_num;
  // 第几周
  let cur_week = Math.ceil(oth_day / 7);
  if (numRes.str_num == 7) {
    cur_week += 1;
  }
  return cur_week;
}

根据年、月、周获取周的日期

getWeekTime(year, month, week) {
  // 获取天数
  var day_num = new Date(year, month, 0).getDate();
  // 该月第一天、最后一天
  const str_day = new Date().setFullYear(year, month - 1, 1);
  const end_day = new Date().setFullYear(year, month, 0);
  //该月第一周、最后一周
  const str_week = new Date(str_day).getDay();
  const end_week = new Date(end_day).getDay();
  const numRes = this.getStrEndDay(str_week, end_week);
  // 该周开始、结束日期
  let str_time = (week - 1) * 7 + 1;
  let end_time = week * 7;
  if (numRes.str_num != 7) {
    str_time += numRes.str_num;
    end_time += numRes.str_num;
  }
  let mm = Number(month);
  mm = mm < 10 ? "0" + mm : mm;
  str_time = str_time < 10 ? "0" + str_time : str_time;
  end_time = end_time < 10 ? "0" + end_time : end_time;
  let strDate = year + "-" + mm + "-" + str_time;
  let endDate = year + "-" + mm + "-" + end_time;
  if (Number(end_time) > day_num) {
    let mm1 = Number(mm) + 1 < 10 ? "0" + (Number(mm) + 1) : Number(mm) + 1;
    let last_num = Number(end_time) - day_num;
    last_num = last_num < 10 ? "0" + last_num : last_num;
    endDate = year + "-" + mm1 + "-" + last_num;
  }
  return {
    strDate,
    endDate
  };
}

二. 实现案例

<template>
  <div>Home</div>
</template>

<script>
export default {
  data() {
    return {};
  },
  mounted() {
    // 自定义一周的开始和结束,计算日期所在月的周数、所在月第几周、所在周的日期范围
    const date = new Date(); //需要计算的日期
    const yy = date.getFullYear();
    const mm = (date.getMonth() + 1 + "").padStart(2, "0");
    const dd = (date.getDate() + "").padStart(2, "0");
    console.log(`计算日期:${yy}${mm}${dd}`);
    const week_count = this.getWeekCount(yy, mm);
    console.log(`${yy}${mm}月总共${week_count}`);
    const week_cur = this.getCurWeek(yy, mm, dd);
    console.log(`${yy}${mm}${dd}日属于本月第${week_cur}`);
    const days = this.getWeekTime(yy, mm, week_cur);
    console.log(
      `${yy}${mm}月第${week_cur}周日期段为${days.strDate} —— ${days.endDate}`
    );
  },
  methods: {
    // 根据月开始日期星期几、月结束日期星期几,计算始周、末周占月的天数(每周周期段:上周六 —— 本周五)
    getStrEndDay(str_week, end_week) {
      // 始周占月的天数:开始周几(str_week) 距离 结束(周五) 有几天
      let str_num = 0;
      switch (str_week) {
        case 1:
          str_num = 5;
          break;
        case 2:
          str_num = 4;
          break;
        case 3:
          str_num = 3;
          break;
        case 4:
          str_num = 2;
          break;
        case 5:
          str_num = 1;
          break;
        case 6:
          str_num = 7;
          break;
        case 7:
          str_num = 6;
          break;
      }
      // 末周占月的天数:开始(周六) 距离 结束周几(end_week) 有几天
      let end_num = 0;
      switch (end_week) {
        case 1:
          end_num = 3;
          break;
        case 2:
          end_num = 4;
          break;
        case 3:
          end_num = 5;
          break;
        case 4:
          end_num = 6;
          break;
        case 5:
          end_num = 7;
          break;
        case 6:
          end_num = 1;
          break;
        case 0:
          end_num = 2;
          break;
      }
      return { str_num, end_num };
    },
    // 根据年月,获取每月有几周
    getWeekCount(year, month) {
      // 获取天数
      var day_num = new Date(year, month, 0).getDate();
      // 该月第一天、最后一天
      const str_day = new Date().setFullYear(year, month - 1, 1);
      const end_day = new Date().setFullYear(year, month, 0);
      //该月第一周、最后一周
      const str_week = new Date(str_day).getDay();
      const end_week = new Date(end_day).getDay();
      // 该月减去第一周、最后一周的天数
      const numRes = this.getStrEndDay(str_week, end_week);
      const oth_day = day_num - numRes.end_num - numRes.str_num;
      // 总周数
      let week_num = oth_day / 7;
      if (numRes.str_num == 7) {
        week_num += 1;
      }
      if (numRes.end_num != 0) {
        week_num += 1;
      }
      return week_num;
    },
    // 根据年月日获取当前周
    getCurWeek(year, month, day) {
      // 该月第一天、最后一天
      const str_day = new Date().setFullYear(year, month - 1, 1);
      const end_day = new Date().setFullYear(year, month, 0);
      //该月第一周、最后一周
      const str_week = new Date(str_day).getDay();
      const end_week = new Date(end_day).getDay();
      // 该月减去第一周的天数
      const numRes = this.getStrEndDay(str_week, end_week);
      const oth_day = day - numRes.str_num;
      // 第几周
      let cur_week = Math.ceil(oth_day / 7);
      if (numRes.str_num == 7) {
        cur_week += 1;
      }
      return cur_week;
    },
    // 根据年、月、周获取周的日期
    getWeekTime(year, month, week) {
      // 获取天数
      var day_num = new Date(year, month, 0).getDate();
      // 该月第一天、最后一天
      const str_day = new Date().setFullYear(year, month - 1, 1);
      const end_day = new Date().setFullYear(year, month, 0);
      //该月第一周、最后一周
      const str_week = new Date(str_day).getDay();
      const end_week = new Date(end_day).getDay();
      const numRes = this.getStrEndDay(str_week, end_week);
      // 该周开始、结束日期
      let str_time = (week - 1) * 7 + 1;
      let end_time = week * 7;
      if (numRes.str_num != 7) {
        str_time += numRes.str_num;
        end_time += numRes.str_num;
      }
      let mm = Number(month);
      mm = mm < 10 ? "0" + mm : mm;
      str_time = str_time < 10 ? "0" + str_time : str_time;
      end_time = end_time < 10 ? "0" + end_time : end_time;
      let strDate = year + "-" + mm + "-" + str_time;
      let endDate = year + "-" + mm + "-" + end_time;
      if (Number(end_time) > day_num) {
        let mm1 = Number(mm) + 1 < 10 ? "0" + (Number(mm) + 1) : Number(mm) + 1;
        let last_num = Number(end_time) - day_num;
        last_num = last_num < 10 ? "0" + last_num : last_num;
        endDate = year + "-" + mm1 + "-" + last_num;
      }
      return {
        strDate,
        endDate
      };
    }
  }
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值