传入年份计算所在年份有多少周,并计算出每周的开始月份和结束月份

一、计算当前日期是星期几

// 周7就是 31-(7-2);就是12月26日
// 周6就是 31-(6-2);就是12月27日
getMyDay(date) {
      var week;
      if (date.getDay() == 0) week = 7;
      if (date.getDay() == 1) week = 1;
      if (date.getDay() == 2) week = 2;
      if (date.getDay() == 3) week = 3;
      if (date.getDay() == 4) week = 4;
      if (date.getDay() == 5) week = 5;
      if (date.getDay() == 6) week = 6;
      return week;
    }

二、 计算一年内多少周,每周的开始日期和结束日期

//value是传入的年份 例如:2023年    
selectOption(value) {
      var year = value.slice(0, 4);
//每年的第一天
      var w1 = this.getMyDay(new Date(year + "-1-1"));
      this.weekOptionsA = [];
      this.weekOptions = [];
      this.dates = [];
      // 周7就是 31-(7-2);就是12月26日
      // 周6就是 31-(6-2);就是12月27日
      // 后面的就是:1+(7-7(1月1的星期))
      let j1 = w1 - 1; //距离前一年的天数
      let YearDayQ = new Date(year + "-1-1").getTime();//计算传入年份第一天的时间戳
      // let q1 = YearDayQ - j1 * 24 * 60 * 60 * 1000; //获取第一周第一天的时间戳
      // let rq = moment(q1).format("YYYY-MM-DD");
      // let xq = this.getMyDay1(new Date(moment(q1).format("YYYY-MM-DD")));
      let index = 1;
      let num = 0;
      let cs = 0;
      let newWeek = [];
      let dates = [];
      let oneDay;
      for (let i = 0; i < 1; i) {
        let q1 = YearDayQ - j1 * 24 * 60 * 60 * 1000 + cs * 24 * 60 * 60 * 1000;//计算传入年份每天的时间戳
        let rq = moment(q1).format("YYYY-MM-DD");
        // let xq = this.getMyDay1(new Date(moment(q1).format("YYYY-MM-DD")));
        // let yr = moment(q1).format("MM")+'月'+moment(q1).format("DD")+'日';
        let yr = moment(q1).format("MM月DD日");
        if (num < 7) {
          if (num == 0) {
            oneDay = yr;
          }
          // newWeek.push("第" + index + "周" + " " + xq + " " + rq);
          dates.push(rq);
          if (num == 6) {
            this.weekOptions.push(
              "第" + index + "周(" + oneDay + "~" + yr + ")"
            );
          }
          num++;
        } else {
          this.weekOptionsA.push(newWeek);
          this.dates.push(dates);
          newWeek = [];
          dates = [];
          index++;
          num = 0;
          cs--;
          // console.log('year',year)
          // console.log('moment(q1).format("YYYY-MM-DD")',moment(q1).format("YYYY-MM-YY"))
          // console.log('moment(q1).format("MM-YY")', rq);
          if (
            year != moment(q1).format("YYYY") ||
            moment(q1).format("MM-YY") == "12-31"
          ) {
            i++;
          }
        }
        cs++;
        // this.weekOptions.push("第" + (index+1) + "周");
      }
      // console.log("this.weekOptionsA: ", this.weekOptionsA);
      // console.log("this.weekOptions: ", this.weekOptions);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值