element-ui 中el-calendar 日历插件获取显示的第一天和最后一天【原创】

需要获取el-calendar 日历组件上的第1天和最后一天。可以通过document.querySelector()方法进行获取dom元素中的值,这样避免计算问题。

获取的过程中主要有两个难点,第1个是处理上1月和下1月的数据,第2个是跨年的数据。

直接贴代码,这个方法可以运用到1、点击上个月,今天、下个月三个按钮,实现点击获取最新的日历组件上的第1天和最后一天。2、通过watch实时监听。

javascript 复制代码
    getRange(date) {
      const today = date ? new Date(date) : new Date();

      // 初始化 startDate 和 endDate
      let startDate = null;
      let endDate = null;

      // 获取日历中的第一个和最后一个单元格
      let firstCell = document.querySelector('#pane-3 > div > div > div.el-calendar__body > table > tbody > tr:nth-child(1) > td:nth-child(1) > div > div > div:nth-child(1) > div > div');
      let lastCell = document.querySelector('#pane-3 > div > div > div.el-calendar__body > table > tbody > tr:nth-child(6) > td:nth-child(7) > div > div > div:nth-child(1) > div > div');

      // 设置 startDate
      if (firstCell && parseInt(firstCell.textContent.trim(), 10) > 1) {
        // 获取前一个月和年份
        let prevMonth = today.getMonth() - 1;
        let year = today.getFullYear();
        if (prevMonth < 0) {
          prevMonth = 11; // 上一年的12月
          year -= 1;
        }
        const monthStr = String(prevMonth + 1).padStart(2, '0'); // 月份从1开始
        const dayStr = String(firstCell.textContent.trim()).padStart(2, '0');
        startDate = `${year}-${monthStr}-${dayStr}`;
      } else {
        // 当月第一天
        const month = today.getMonth() + 1;
        const monthStr = String(month).padStart(2, '0');
        startDate = `${today.getFullYear()}-${monthStr}-01`;
      }

      // 设置 endDate
      if (lastCell && parseInt(lastCell.textContent.trim(), 10) < 30) {
        // 获取下一个月和年份
        let nextMonth = today.getMonth() + 1;
        let year = today.getFullYear();
        if (nextMonth > 11) {
          nextMonth = 0; // 下一年的1月
          year += 1;
        }
        const monthStr = String(nextMonth + 1).padStart(2, '0'); // 月份从1开始
        const dayStr = String(lastCell.textContent.trim()).padStart(2, '0');
        endDate = `${year}-${monthStr}-${dayStr}`;
      } else {
        // 当月最后一天
        const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate();
        const month = today.getMonth() + 1;
        const monthStr = String(month).padStart(2, '0');
        endDate = `${today.getFullYear()}-${monthStr}-${String(lastDay).padStart(2, '0')}`;
      }

      // 返回包含 startDate 和 endDate 的对象
      return {
        startDate,
        endDate
      };
    }

运行测试截图

相关推荐
pepedd86443 分钟前
全面解析this-理解this指向的原理
前端·javascript·trae
渔夫正在掘金43 分钟前
神奇魔法类:使用 createMagicClass 增强你的 JavaScript/Typescript 类
前端·javascript
雲墨款哥44 分钟前
一个前端开发者的救赎之路-JS基础回顾(三)-Function函数
前端·javascript
汪子熙1 小时前
深入理解 TypeScript 的 /// <reference /> 注释及其用途
前端·javascript
Spider_Man1 小时前
面试官的 JS 继承陷阱,你能全身而退吗?🕳️
前端·javascript·面试
全宝1 小时前
【前端特效系列】css+js实现聚光灯效果
javascript·css·html
ciku1 小时前
Spring AI Starter和文档解读
java·人工智能·spring
全栈软件开发1 小时前
PHP域名授权系统网站源码_授权管理工单系统_精美UI_附教程
开发语言·ui·php·php域名授权·授权系统网站源码
HYI1 小时前
「三年了,今晚突然开窍!」 一个拖拽排序的顿悟时刻
javascript·vue.js