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

相关推荐
是大林的林吖2 小时前
解决 elementui el-cascader组件懒加载时存在选中状态丢失的问题?
前端·javascript·elementui
一 乐2 小时前
个人博客|博客app|基于Springboot+微信小程序的个人博客app系统设计与实现(源码+数据库+文档)
java·前端·数据库·spring boot·后端·小程序·论文
sTone873752 小时前
Android Room部件协同使用
android·前端
晴殇i2 小时前
前端代码规范体系建设与团队落地实践
前端·javascript·面试
用户74054639943092 小时前
Vite 库模式输出 ESM 格式时的依赖处理方案
前端·vite
开发者小天2 小时前
React中使用useParams
前端·javascript·react.js
lichong9512 小时前
Android studio release 包打包配置 build.gradle
android·前端·ide·flutter·android studio·大前端·大前端++
Lyuing2 小时前
el-input数字类型禁止+-符号输入
vue.js
nvvas2 小时前
npm : 无法加载文件 D:\nvm\nodejs\npm.ps1,因为在此系统上禁止运行脚本问题解决
前端·npm·node.js