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
      };
    }

运行测试截图

相关推荐
计算机学姐2 分钟前
基于SpringBoot的小型民营加油站管理系统
java·vue.js·spring boot·后端·mysql·spring·tomcat
Elastic 中国社区官方博客6 分钟前
JavaScript 中使用 Elasticsearch 的正确方式,第一部分
大数据·开发语言·javascript·数据库·elasticsearch·搜索引擎·全文检索
小雅痞7 分钟前
[Java][Leetcode middle] 151. 反转字符串中的单词
java·leetcode
ThomasChan1238 分钟前
Win10 安装单机版ES(elasticsearch),整合IK分词器和安装Kibana
java·大数据·elasticsearch·搜索引擎·全文检索·jenkins·es
万物得其道者成17 分钟前
从零开始创建一个 Next.js 项目并实现一个 TodoList 示例
开发语言·javascript·ecmascript
向哆哆26 分钟前
Eclipse Java 开发调优:如何让 Eclipse 运行更快?
java·ide·eclipse
爱晒太阳的小老鼠33 分钟前
策略模式-枚举实现
java·策略模式
子朔不言36 分钟前
MH22D3开发高级UI应用,适配arm2d驱动
mcu·ui·arm2d·mh22d3·新龙微
77tian40 分钟前
设计模式的原理及深入解析
java·开发语言·单例模式·设计模式·代理模式·享元模式·原型模式
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Expanding Cards (展开式卡片)
javascript·vue.js·ecmascript