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

相关推荐
Sailing13 分钟前
🚀 别再乱写 16px 了!CSS 单位体系已经进入“计算时代”,真正的响应式布局
前端·css·面试
喝水的长颈鹿29 分钟前
【大白话前端 03】Web 标准与最佳实践
前端
爱泡脚的鸡腿30 分钟前
Node.js 拓展
前端·后端
左夕2 小时前
分不清apply,bind,call?看这篇文章就够了
前端·javascript
Zha0Zhun2 小时前
一个使用ViewBinding封装的Dialog
前端
兆子龙2 小时前
从微信小程序 data-id 到 React 列表性能优化:少用闭包,多用 data-*
前端
滕青山2 小时前
文本行过滤/筛选 在线工具核心JS实现
前端·javascript·vue.js
时光不负努力2 小时前
编程常用模式集合
前端·javascript·typescript
时光不负努力2 小时前
ts+vue3开发规范
vue.js·typescript
恋猫de小郭3 小时前
Apple 的 ANE 被挖掘,AI 硬件公开,宣传的 38 TOPS 居然是"数字游戏"?
前端·人工智能·ios