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>

效果图

相关推荐
还是大剑师兰特5 小时前
Vue3 报错:computed value is readonly 解决方案
前端·vue.js
gis开发6 小时前
cesium 中添加鹰眼效果
前端·javascript
bluceli6 小时前
JavaScript动态导入与代码分割:优化应用加载性能的终极方案
javascript
kyriewen6 小时前
原型与原型链:JavaScript 的“家族关系”大揭秘
前端·javascript·ecmascript 6
滴滴答答哒6 小时前
layui表格头部按钮 加入下拉选项
前端·javascript·layui
乌索普-6 小时前
基于vue2的简易购物车
开发语言·前端·javascript
走粥6 小时前
使用indexOf查找对象结合Pinia持久化引发的问题
开发语言·前端·javascript
北寻北爱7 小时前
前端加密解密- base64、md5、sha256、AES
前端·vue.js
不甜情歌7 小时前
搞懂 Promise:告别回调嵌套,再也不怕异步代码乱成麻
前端·javascript
spencer_tseng8 小时前
Vue node_modules
javascript·vue.js