el-date-picker 限制开始时间和结束时间

el-date-picker 限制开始时间和结束时间

需求:el-date-picker 月份限制开始时间和结束时间

开始时间:202307

结束时间:202407

代码实现

vue 页面

javascript 复制代码
  <el-form-item label="月份" prop="monthList">
          <el-date-picker v-model="allForm.monthList" type="monthrange" range-separator="至" start-placeholder="开始月份"
            end-placeholder="结束月份" value-format="yyyy-MM" :picker-options="pickerOptions">
          </el-date-picker>
   </el-form-item>

script

javascript 复制代码
<script>
export default {
 data() {
    return {
      allForm: {},
      allRules: {
        monthList: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          const now = new Date()
          const year = now.getFullYear()
          const month = now.getMonth()

          // 去年当前月份的前一个月
          const startYear = year - 1
          const startMonth = month === 0 ? 11 : month - 1
          const startDate = new Date(startYear, startMonth, 1)

          // 当前月份的前一月
          const endYear = year
          const endMonth = month === 0 ? 11 : month - 1
          const endDate = new Date(endYear, endMonth, 1)

          return (
            time.getTime() < startDate.getTime() || time.getTime() > endDate.getTime()
          )
        }
      }
      
    }
  }
}
</script>

效果图

相关推荐
_Kayo_1 天前
TypeScript 学习笔记2
前端·javascript·typescript
ss2731 天前
SpringBoot+vue养老院运营管理系统
vue.js·spring boot·后端
用户841794814561 天前
vue 甘特图 vxe-gantt table 依赖线的使用,配置连接线
vue.js
前端 贾公子1 天前
Eruda:移动端网页调试利器
前端·javascript·vue.js
Hashan1 天前
Elpis:抽离业务代码,发布NPM包
前端·javascript·vue.js
用户47949283569151 天前
0.1加0.2为什么不等于0.3-答不上来的都挂了
前端·javascript·面试
rit84324991 天前
C#实现的远程控制系统
前端·javascript·c#
南山安1 天前
React学习:Vite+React 基础架构分析
javascript·react.js·面试
JS_GGbond1 天前
JavaScript事件循环:餐厅里的“宏任务”与“微任务”
开发语言·javascript·ecmascript
脾气有点小暴1 天前
uniapp开发APP 内嵌外部 HTTPS 链接的实现方案
vue.js·uni-app