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>

效果图

相关推荐
avi911110 小时前
[AI教做人]AI平台做2项目;一个3D模型展示,另一个框架多人
javascript·人工智能·ai·3d模型·3d引擎·顶点和法线
kyriewen10 小时前
我用Claude Code两天干完了团队两周的排期——周报发出去那一刻我就后悔了
前端·javascript·ai编程
用户9385156350711 小时前
手写一个 LLM Harness 框架:用工程化手段把大模型幻觉踩在脚下
javascript·人工智能·后端
油丶酸萝卜别吃11 小时前
jquery-ajax.js 说明文档
前端·javascript·jquery
windliang11 小时前
Claude Code 源码分析(九):子 Agent 如何分叉、继续与回到父会话
前端·javascript·面试
柒和远方12 小时前
V063: TS 面试必考:interface 与 type 的四大差异,与 LLM Harness 的自动化择优
前端·javascript
宿67412 小时前
vue3-env环境
前端·vue.js
汉堡大王952713 小时前
Vue 3 + TS + Element Plus 实战:如何从零搭建企业级违章记录管理 SaaS 前端
前端·javascript·vue.js
海天鹰13 小时前
屏幕颜色检测
javascript·html5
用户79172867619713 小时前
opencode-plugin-peers完全指南:OpenCode如何实现 Claude Code式跨会话消息
javascript