通过月,获取所有周

需求:通过参数某年某月,获取该月的所有周

// 通过传入年、月,获取该月有几周以及具体日期
const getWeekData = (year, month) => {
  
    var new_year = year; //取当前的年份
    var new_month = month++; //取下一个月的第一天,方便计算(最后一天不固定)
    if (new_month < 10) {
      new_month = "0" + new_month;
    }
    var weekDay = [
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六",
      "星期天",
    ];
    if (new_month > 12) {
      new_month -= 12; //月份减
      new_year++; //年份增
    }
    var first_date = new Date(new_year, new_month, 1); //取当年当月中的第一天-时间格式
    var last_Data = new Date(
      first_date.getTime() - 1000 * 60 * 60 * 24
    ).getDate(); //获取当月最后一天日期
    //当月第一天是周几
    var firstzhouji =
      new Date(new_year + "/" + new_month + "/" + 1).getDay() == 0
        ? "星期天"
        : weekDay[new Date(new_year + "/" + new_month + "/" + 1).getDay() - 1];
    //当月最后一天是周几
    var lastzhouji =
      new Date(new_year + "/" + new_month + "/" + last_Data).getDay() == 0
        ? "星期天"
        : weekDay[
            new Date(new_year + "/" + new_month + "/" + last_Data).getDay() - 1
          ];
    var firsttime = ""; //第一周有几天
    if (firstzhouji == "星期一") {
      firsttime = 7;
    }
    if (firstzhouji == "星期二") {
      firsttime = 6;
    }
    if (firstzhouji == "星期三") {
      firsttime = 5;
    }
    if (firstzhouji == "星期四") {
      firsttime = 4;
    }
    if (firstzhouji == "星期五") {
      firsttime = 3;
    }
    if (firstzhouji == "星期六") {
      firsttime = 2;
    }
    if (firstzhouji == "星期天") {
      firsttime = 1;
    }

    var lasttime = ""; //最后一周有几天
    if (lastzhouji == "星期一") {
      lasttime = 1;
    }
    if (lastzhouji == "星期二") {
      lasttime = 2;
    }
    if (lastzhouji == "星期三") {
      lasttime = 3;
    }
    if (lastzhouji == "星期四") {
      lasttime = 4;
    }
    if (lastzhouji == "星期五") {
      lasttime = 5;
    }
    if (lastzhouji == "星期六") {
      lasttime = 6;
    }
    if (lastzhouji == "星期天") {
      lasttime = 7;
    }

    // 前后两周  加上 剩余周数  得出总周数
    var contime = 2 + (last_Data - lasttime - firsttime) / 7;

    //得出每周对应的日期
    var zhouArry = [];

    for (var i = 1; i <= contime; i++) {
      var strTime = "";
      var lastTime = "";
      if (i == 1) {
        strTime = year + "-" + new_month + "-" + "01";
        let aa = 1 + firsttime - 1;
        if (aa < 10) {
          aa = "0" + aa;
        }
        lastTime = year + "-" + new_month + "-" + aa;
      } else if (i == contime) {
        let bb = last_Data - lasttime + 1;
        if (bb < 10) {
          bb = "0" + bb;
        }
        strTime = year + "-" + new_month + "-" + bb;
        lastTime =
          year +
          "-" +
          new_month +
          "-" +
          (last_Data < 10 ? "0" + last_Data : last_Data);
      } else {
        strTime = addDate(zhouArry[zhouArry.length - 1].endTime, 1);
        lastTime = addDate(zhouArry[zhouArry.length - 1].endTime, 7);
      }
      let chBeginTimeMonth = 0;
      let chBeginTimeDate = 0;

      let chEndTimeDate = 0;
      if (strTime.substring(8, 9) === 0) {
        // 转换开始日期的具体日
        chBeginTimeDate = strTime.substring(9, 10);
      } else {
        chBeginTimeDate = strTime.substring(8, 10);
      }
      if (strTime.substring(5, 7) > 9) {
        // 转换开始日期的具体月
        chBeginTimeMonth = strTime.substring(5, 7);
      } else {
        chBeginTimeMonth = strTime.substring(6, 7);
      }
      if (lastTime.substring(8, 9) === 0) {
        chEndTimeDate = lastTime.substring(9, 10);
      } else {
        chEndTimeDate = lastTime.substring(8, 10);
      }
      zhouArry.push({
        weeknum: i,
        beginTime: strTime,
        endTime: lastTime,
        period:
          chBeginTimeMonth +
          "月" +
          chBeginTimeDate +
          "日" +
          " " +
          "-" +
          " " +
          chEndTimeDate +
          "日",
      });

      //日期增加 接受两个参数, 传入的时间,传入时间增加的天数
      function addDate(date, days) {
        if (days == undefined || days == "") {
          days = 1;
        }
        var date = new Date(date);
        date.setDate(date.getDate() + days);
        var month = date.getMonth() + 1;
        if (month < 10) {
          month = "0" + month;
        }
        var day = date.getDate();
        if (day < 10) {
          day = "0" + day;
        }
        return date.getFullYear() + "-" + month + "-" + day;
      }
    }
    return zhouArry;
  };
红框中为展示的周信息

转载自https://www.jianshu.com/p/f06a9252de88

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容