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>

效果图

相关推荐
子非鱼9211 小时前
两栏布局、三栏布局、水平垂直居中
前端·javascript·css
追光少年33226 小时前
Learning Vue 读书笔记 Chapter 4
前端·javascript·vue.js
软件2056 小时前
【Vite + Vue + Ts 项目三个 tsconfig 文件】
前端·javascript·vue.js
LCG元8 小时前
Vue.js组件开发-如何实现异步组件
前端·javascript·vue.js
wl85118 小时前
vue入门到实战 三
前端·javascript·vue.js
ljz20168 小时前
本地搭建deepseek-r1
前端·javascript·vue.js
傻小胖9 小时前
vue3中Teleport的用法以及使用场景
前端·javascript·vue.js
wl851110 小时前
Vue 入门到实战 七
前端·javascript·vue.js
三气归来10 小时前
轮播库-swiper使用案例
javascript
LCG元10 小时前
Vue.js组件开发-使用Vue3如何实现上传word作为打印模版
前端·vue.js·word