elemetui中el-date-picker限制开始结束日期只能选择当月

项目中有个查询条件,功能要求的是选择开始和结束日期,开始结束日期只能选择当前月份。

相关文章:elementui日期选择器设置开始时间不能大于结束时间

项目用的elementui,实现方式如下所示

html 复制代码
<el-date-picker v-model="gateAccessQueryData.startTime" size="small" type="date" placeholder="开始时间" :picker-options="pickerOptionsStart" value-format="yyyy-MM-dd"></el-date-picker>
<el-date-picker v-model="gateAccessQueryData.endTime" size="small" type="date" placeholder="结束时间" :picker-options="pickerOptionsEnd" value-format="yyyy-MM-dd"></el-date-picker>
javascript 复制代码
data() {
  return {
    gateAccessQueryData: {
      startTime: '',
      endTime: ''
    },
    // 开始日期限制
    pickerOptionsStart: {
      disabledDate: (time) => {
        const current = new Date()
        const firstDay = new Date(current.getFullYear(), current.getMonth(), 1)
        const lastDay = new Date(current.getFullYear(), current.getMonth() + 1, 0)
        // 默认限制:不在当月范围内
        let isNotInCurrentMonth = time.getTime() < firstDay.getTime() || time.getTime() > lastDay.getTime()
        // 额外限制:如果已选结束时间,不能选择结束时间之后的日期
        if (this.gateAccessQueryData.endTime) {
          return isNotInCurrentMonth || time.getTime() > new Date(this.gateAccessQueryData.endTime).getTime()
        }
        return isNotInCurrentMonth
      }
    },
    // 结束日期限制
    pickerOptionsEnd: {
      disabledDate: (time) => {
        const current = new Date()
        const firstDay = new Date(current.getFullYear(), current.getMonth(), 1)
        const lastDay = new Date(current.getFullYear(), current.getMonth() + 1, 0)
        // 默认限制:不在当月范围内
        let isNotInCurrentMonth = time.getTime() < firstDay.getTime() || time.getTime() > lastDay.getTime()
        // 额外限制:如果已选开始时间,不能选择开始时间之前的日期
        if (this.gateAccessQueryData.startTime) {
          return isNotInCurrentMonth || time.getTime() <= new Date(this.gateAccessQueryData.startTime).getTime() - 86400000
        }
        return isNotInCurrentMonth
      }
    }
  }
}

原文围观地址 https://www.sharedbk.com/post/286.htmlhttps://www.sharedbk.com/post/286.html

相关推荐
福兮说4 小时前
前端下载大文件,点了按钮半天没反应:fetch + blob 的三个问题和三种替代写法
前端·javascript·性能优化·文件下载
GreenTea4 小时前
我把 Agent 的 while 循环拆掉了:一种你可能没想到的 Agent 架构
前端·后端·架构
科技观察哨4 小时前
六足平台选型与纳米定位系统集成:HEB-640六自由度位移台在半导体光刻对准中的参数边界与国产替代评估
前端·人工智能
niucloud-admin5 小时前
JAVA V6 多商户商城 开发文档——web前端
前端
颜进强5 小时前
16 · NestJS LifecycleEvents 生命周期事件:五个钩子、三条边界,和 rag-server 的优雅停机
前端·后端·ai编程
今年下半年6 小时前
【VUE】整合腾讯地图、自定义区域边界、村委名称及资产统计(放大显示资产点位)
前端·javascript·vue.js
郑州光合科技余经理7 小时前
同城电商系统:库存变更怎么同步到订单
java·开发语言·前端·后端·uni-app·php·ai编程
前端snow8 小时前
ai agent --- DeepAgents 中间件
前端
data analyse 4569 小时前
报表能自定义的国内统计工具有哪些
前端·数据分析