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

相关推荐
CXH728几秒前
架构师的登山之路|第十二站:服务网格 Istio——未来的标配,还是复杂过头?
前端·javascript·istio
脾气有点小暴23 分钟前
详解 HTML Image 的 mode 属性:图像显示模式的灵活控制
前端·html·uniapp
爱吃无爪鱼36 分钟前
03-Bun vs Node.js:JavaScript 运行时的新旧之争
javascript·vue.js·react.js·npm·node.js
0思必得01 小时前
[Web自动化] 开发者工具性能(Performance)面板
运维·前端·自动化·web自动化·开发者工具
心灵的制造商1 小时前
el-tree左侧新增类别和删除类别实例代码
前端·javascript·vue.js
爱吃无爪鱼1 小时前
01-前端开发快速入门路线图
javascript·css·vue.js·typescript·前端框架·npm·node.js
冴羽1 小时前
不知道怎么写 Nano Banana Pro 提示词?分享你一个结构化示例,复刻任意图片
前端·人工智能·aigc
IT_陈寒1 小时前
JavaScript 性能优化:7个 V8 引擎隐藏技巧让你的代码提速200%
前端·人工智能·后端
脾气有点小暴1 小时前
uniapp通用单张图片上传组件
前端·javascript·vue.js·uni-app·uniapp
小菜今天没吃饱1 小时前
DVWA-XSS(stored)
前端·网络安全·xss·dvwa