vue element-ui月份范围选择器,禁用未来月份,跨度为12个月

文章介绍了如何在Vue2项目中使用ElementUI的日期组件`el-date-picker`,特别是`monthrange`类型,来设置月份范围选择,并通过`disabledDate`属性限制可选日期范围,确保用户只能选择当前月份之前的一年内的月份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用得vue2的写法哦,
效果如下图:
在这里插入图片描述

直接上代码:

引用ElementUI的月份组件:

<el-date-picker
                            v-model="queryParams.reportDate"
                                type="monthrange"
                                :editable="false"
                                :clearable="false"
                                format="yyyy 年 MM 月"
                                value-format="yyyy-MM"
                                range-separator="至"
                                start-placeholder="开始月份"
                                end-placeholder="结束月份" 
                                :picker-options = "monthpickoption">
                                </el-date-picker>
export default {
    components:{LineChart,yearPicker,DateWeekRange},
    data() {
        return {
        trendData: {
                date: []
            },
            minDate: null,
            maxDate: null,
            monthpickoption:{
            disabledDate: (time) => {
              if (this.minDate !== null && this.maxDate === null) {
                  let minMonth = this.minDate.getMonth(),
                  lastYear = new Date(this.minDate).setMonth(minMonth - 11),
                  nextYear = new Date(this.minDate).setMonth(minMonth + 11);
                let minTime = this.minDate.getTime()
                let nextTime = new Date().setMonth(new Date().getMonth() + 11)
                let lastTime = new Date().setMonth(new Date().getMonth() - 11)
               if (minTime <= nextTime || minTime >= lastTime){
                  //开始日期少于当前月+12个月 并且大于当前月-12个月,则表示只限制前面的范围
                  return time.valueOf() > new Date().getTime() || time.valueOf() > nextYear.valueOf() || time.valueOf() < lastYear.valueOf()
                }
                else {
                  // 只能选 minDate 前后一年的范围
                  return time.valueOf() < lastYear.valueOf() || time.valueOf() > nextYear.valueOf();
                }
              }else {
                let startMonth = this.trendData.date[0]
                let month = new Date(startMonth).getMonth()
                let lastMonth = new Date(startMonth).setMonth(month - 11)
                //如果有默认值,只禁用开始日期,因为已经限定了禁用未来范围,且默认值已经为12个月范围
                return this.monthPick(time) || time < lastMonth.valueOf();
              }
            },
            onPick: ({minDate, maxDate}) => {
              this.minDate = minDate;
              this.maxDate = maxDate;
            }
            },
         }
      },
      methods: {
      	monthPick(time){
            // 获取当前的月份信息
            const date = new Date() // 获取当前的时间基本信息,值是这样的: Tue Jul 20 2021 14:59:43 GMT+0800 (中国标准时间)
            const year = date.getFullYear() // 获取当前年份,值是这样的: 2021
            let month = date.getMonth() + 1 // 获取当前月份,值是这样的: 6   getMonth()方法返回值是0-11,也就是1月份到12月份,所以要加上1,才是当前月份
            if (month >= 1 && month <= 9) {
              // 如果是1月到9月就要在前面补上一个0   比如:02、07    月份这样表示
              month = '0' + month
            }
            const nowDate = year.toString() + month.toString() // 转换成字符串拼接,最终得到年和月,比如此时的时间是2021年7月20号,所以nowDate的值是:202107
 
            // 获取时间选择器的月份信息
            const timeyear = time.getFullYear() // 获取时间选择器的年份(有很多)
            let timemonth = time.getMonth() + 1 // 与上面同理
            if (timemonth >= 1 && timemonth <= 9) {
              timemonth = '0' + timemonth
            }
            const elTimeData = timeyear.toString() + timemonth.toString()
 
            // 返回,时间选择器的日期月份大于当前日期的月份,又因为disabledDate函数是控制月份禁用不可选
            // 所以,最终就是:时间选择器的月份大于当前的月份,就都禁用掉,所以就实现了最终效果:
            // 大于等于当前月份都不可选
            return elTimeData > nowDate // 这里虽然是字符串,但是弱类型语言js会做一个转换,是可以比较大小的如: '202107' > '202008'
          },
      }

参考地址:
https://blog.youkuaiyun.com/ITERCHARLIE/article/details/127246715

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xingchen678

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值