前端js/vue根据年月获取当前周次及每周开始和结束日期

效果如下:

[
    {
        "label":"第一周",
        "value":1,
        "date":[
            "2023-06-26",
            "2023-07-02"
        ]
    },
    {
        "label":"第二周",
        "value":2,
        "date":[
            "2023-07-03",
            "2023-07-09"
        ]
    },
    {
        "label":"第三周",
        "value":3,
        "date":[
            "2023-07-10",
            "2023-07-16"
        ]
    },
    {
        "label":"第四周",
        "value":4,
        "date":[
            "2023-07-17",
            "2023-07-23"
        ]
    },
    {
        "label":"第五周",
        "value":5,
        "date":[
            "2023-07-24",
            "2023-07-30"
        ]
    },
    {
        "label":"第六周",
        "value":6,
        "date":[
            "2023-07-31",
            "2023-08-06"
        ]
    }
]

调用:

allweeks('2023-07');
this.allweeks('2023-07');

JS:

/** 本月的每一周*/
function allWeeks(now_month) {
  let week_array = [];
  let today = (now_month != null && now_month != undefined && now_month != '') ? new Date(Date.parse(now_month)) : new Date();;
  let year = today.getFullYear();
  let month = today.getMonth();
  let i = 0;
  let weekTimes = 1;

  let start = new Date(year, month, 1); // 得到当月第一天
  let end = new Date(year, month+1, 6); // 用于比较
  let start_day = start.getDay(); // 当月第一天是周几

  switch(start_day)
  {
    case 0: i = 0 - 5; break;
    case 1: i = 1; break;
    case 2: i = 0; break;
    case 3: i = 0 - 1; break;
    case 4: i = 0 - 2; break;
    case 5: i = 0 - 3; break;
    case 6: i = 0 - 4; break;
  }

  while ( new Date(year, month, i + 6) <= end) {
    week_array.push({"label":"第"+numberToChinese(weekTimes)+"周","value":weekTimes,"date":[new Date(year, month, i).format("yyyy-MM-dd"),new Date(year, month, i + 6).format("yyyy-MM-dd")]})
    i += 7;
    weekTimes++;
  }

  console.log(week_array);
  return week_array;
}

/** 数字转中文 */
function numberToChinese(num) {
  const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  const chineseUnits = ['', '十', '百', '千'];

  if (num === 0) {
    return chineseNums[0];
  }

  let chineseStr = '';
  let unitIndex = 0;

  while (num > 0) {
    const digit = num % 10;
    if (digit !== 0) {
      // 处理非零数字
      chineseStr = chineseNums[digit] + chineseUnits[unitIndex] + chineseStr;
    } else if (chineseStr.charAt(0) !== chineseNums[0]) {
      // 处理连续的零,只保留一个零
      chineseStr = chineseNums[0] + chineseStr;
    }

    num = Math.floor(num / 10);
    unitIndex++;
  }

  return chineseStr;
}

/** 日期格式化 */
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.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;
}

VUE:

 <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="月份" prop="month">
        <el-date-picker v-model="queryParams.month" placeholder="请选择月份" style="width: 240px" value-format="yyyy-MM" type="month" range-separator="-"/>
      </el-form-item>
      <el-form-item label="周次" prop="weekTimes">
        <el-select v-model="queryParams.weekTimes" placeholder="请选择周次" clearable>
          <el-option v-for="dict in this.allWeeks(queryParams.month)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
methods:{
	/** 本月的每一周*/
    allWeeks(now_month) {
      let week_array = [];
      let today = (now_month != null && now_month != undefined && now_month != '') ? new Date(Date.parse(now_month)) : new Date();;
      let year = today.getFullYear();
      let month = today.getMonth();
      let i = 0;
      let weekTimes = 1;

      let start = new Date(year, month, 1); // 得到当月第一天
      let end = new Date(year, month+1, 6); // 用于循环时比较
      let start_day = start.getDay(); // 当月第一天是周几

      switch(start_day)
      {
        case 0: i = 0 - 5; break;
        case 1: i = 1; break;
        case 2: i = 0; break;
        case 3: i = 0 - 1; break;
        case 4: i = 0 - 2; break;
        case 5: i = 0 - 3; break;
        case 6: i = 0 - 4; break;
      }

      while ( new Date(year, month, i + 6) <= end) {
        week_array.push({"label":"第"+this.numberToChinese(weekTimes)+"周","value":weekTimes,"date":[this.dateFmt(new Date(year, month, i),"yyyy-MM-dd"),this.dateFmt(new Date(year, month, i + 6),"yyyy-MM-dd")]})
        i += 7;
        weekTimes++;
      }

      console.log(week_array);
      return week_array;
    },
    /** 数字转中文 */
    numberToChinese(num) {
      const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
      const chineseUnits = ['', '十', '百', '千'];

      if (num === 0) {
        return chineseNums[0];
      }

      let chineseStr = '';
      let unitIndex = 0;

      while (num > 0) {
        const digit = num % 10;
        if (digit !== 0) {
          // 处理非零数字
          chineseStr = chineseNums[digit] + chineseUnits[unitIndex] + chineseStr;
        } else if (chineseStr.charAt(0) !== chineseNums[0]) {
          // 处理连续的零,只保留一个零
          chineseStr = chineseNums[0] + chineseStr;
        }

        num = Math.floor(num / 10);
        unitIndex++;
      }

      return chineseStr;
    },
    /** 日期格式化 */
    dateFmt(date,fmt) {
      let 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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值