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>

效果图

相关推荐
晓说前端4 小时前
第一篇:为什么学TypeScript?—— 优势、场景与环境搭建
javascript·ubuntu·typescript
ZC跨境爬虫6 小时前
模块化烹饪小程序开发日记 Day7:(菜谱详情接口开发与JSON数据读取全流程)
前端·javascript·css·ui·微信小程序·json
এ慕ོ冬℘゜6 小时前
JS 前端基础面试题
开发语言·前端·javascript
张元清9 小时前
驯服 React 里的 DOM 事件:useEventListener、useEventEmitter、useKeyModifier、useTextSelect
前端·javascript·面试
古韵9 小时前
�������� JavaScript �հ�����ԭ����ʵս
javascript
代码熊崽的编程森林9 小时前
vue + onlyoffice 自定义插件的实现(OnlyOffice 插件:AI 智能编辑)。
前端·javascript·vue.js
Lucky_Turtle10 小时前
【Vue】element plus Slider小数组件设置顺滑程度
前端·javascript·vue.js
Dxy123931021610 小时前
js中Math.min.apply()详解
开发语言·javascript
砍材农夫11 小时前
物联网 基于netty控制报文结构(发布与接收)
java·开发语言·前端·javascript·物联网
上单带刀不带妹11 小时前
Vue3 中 getCurrentInstance() 与 proxy 详解
前端·javascript·vue.js