当前位置: 首页 > news >正文

el-date-picker 禁用一个月前、一个月后(当天之后)的时间 datetimerange

文章目录

  • 功能需求
    • 今天是 2025-01-09
    • 示例1
    • 示例2
  • 代码 Vue2

功能需求

时间范围选择器,最大时间选择尺度为一个月。

今天是 2025-01-09

示例1

选择 2025-01-02 日
禁用未来日期(2025-01-09之后日期)
禁用上月2号(31日之前)之前的日期
在这里插入图片描述

示例2

选择2024-12-02
禁用31日之前日期
禁用31日之后日期
在这里插入图片描述

代码 Vue2

<template><el-date-pickerv-model="dates"type="datetimerange"range-separator=""start-placeholder="开始日期"end-placeholder="结束日期":picker-options="pickerOptions"></el-date-picker>
</template><script>
export default {data() {return {dates: [],currentTime: '',pickerOptions: {onPick: ({minDate, maxDate}) => {if (maxDate) {this.currentTime = ''return}this.currentTime = minDate.getTime()},disabledDate: (time) => {const now = new Date()// 禁用当前时间之后的时间if (time.getTime() > now) return true// 禁用今天23:59:59 之后的时间;如果没有时、分、秒选择器,则不需要对时分秒进行特殊处理// if (time.getTime() > new Date(now.toLocaleDateString() + ' 23:59:59')) return trueconst limitDays = 31 * 24 * 3600 * 1000const minTime = this.currentTime - limitDaysconst maxTime = this.currentTime + limitDays// 禁用一月之前的时间if (time.getTime() < minTime) return true// 禁用一月之后 || 当前时间今天之后的时间// 添加this.currentTime判断: 如果未选择开始日期 或者 选择了结束时间时 this.currentTime 是 空的,if (this.currentTime && time.getTime() > maxTime) return truereturn false}}};},methods: {checkDisabled(date) {// 检查是否超过当前日期一个月const oneMonthLater = new Date(this.currentDate);oneMonthLater.setMonth(oneMonthLater.getMonth() + 1);if (date >= oneMonthLater) {this.disabledDays.push(date);}},},watch: {dates(newDates) {// 当日期范围改变时,更新禁用日期和当前日期this.currentDate = newDates[1];this.checkDisabled(newDates[0]);this.checkDisabled(newDates[1]);// 清空已禁用的未来日期this.disabledDays = [];for (let date of this.dates) {this.checkDisabled(date);}},},
};
</script>
http://www.lryc.cn/news/518327.html

相关文章:

  • 【C】编译与链接
  • Github上传项目
  • webrtc之rtc::ArrayView<const uint8_t>
  • Zemax 序列模式下的扩束器
  • Flink系统知识讲解之:如何识别反压的源头
  • RK3568平台(USB篇)禁用USB端口
  • 洛谷 P3000 [USACO10DEC] Cow Calisthenics G
  • Web渗透测试之XSS跨站脚本攻击 盲打 详解
  • 经典编程题:服务器广播
  • 【网络协议】静态路由详解
  • 朝天椒USB服务器在银泰证券虚拟化超融合场景的应用案例
  • .NET framework、Core和Standard都是什么?
  • FairGuard游戏安全2024年度报告
  • JetBrains IDEs和Visual Studio Code的对比
  • 文件剪切走:深度解析与高效恢复策略
  • Win32汇编学习笔记09.SEH和反调试
  • [人工智能]CSDN创作助手体验
  • vue3中el-table实现多表头并表格合并行或列
  • HTML+CSS+JS制作中国传统节日主题网站(内附源码,含5个页面)
  • 时空笔记:CBEngine(微观交通模拟引擎)
  • 【LeetCode】力扣刷题热题100道(26-30题)附源码 轮转数组 乘积 矩阵 螺旋矩阵 旋转图像(C++)
  • 【C++】字符串的 += 和 + 运算详解
  • 多模态大模型部署:结合dify
  • Matlab Steger提取条纹中心(非极大值抑制)
  • springboot + vue+elementUI图片上传流程
  • LabVIEW 系统诊断
  • 韩国机场WebGIS可视化集合Google遥感影像分析
  • springCloudGateWay使用总结
  • 使用new Vue创建Vue 实例并使用$mount挂载到元素上(包括el选项和$mount区别)
  • GTX750Ti打DP补丁