el-date-picker自定义只能选中当前月份和半年内月份等

需求:el-date-picker只能选中当前月期和当前月期往前半年,其他时间就禁用了不让选择了,因为没数据哈哈。当然也可以选择往前一年等。

一、效果

二、写个日期选择器

:picker-options:日期选项

value-format:选择后的格式

@change:事件改变后触发的函数

html 复制代码
<el-date-picker
          type="months"
          v-model="monthTimeData"
          placeholder="选择一个或多个日期"
          :picker-options="pickerOptions"
          value-format="yyyyMM"
          @change="monthTime"
        >
        </el-date-picker>

三、data的值

javascript 复制代码
      monthTimeData: [], // 绑定的日期值
      currentDate: new Date(), // 当前时间

四、computed

this.currentDate.getMonth() - 4:注意!!!这个-4,就是当前月往前可选的几个月,

-6就是不算上当前月往前可以选6个月,实在不懂可以自己测一下很简单的~

@change时间就是个函数,值就是选择后的值,这边我就不写了

javascript 复制代码
  computed: {
    pickerOptions() {
      const startMonth = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() - 4
      );
      const endMonth = this.currentDate;
      
      return {
        disabledDate(time) {
          return time < startMonth || time > endMonth;
        },
      };
    },
  },

文章到此结束,希望对你有所帮助~

相关推荐
岱宗夫up2 小时前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
紫陌涵光2 小时前
112. 路径总和
java·前端·算法
漠月瑾-西安2 小时前
CVE-2025-55182漏洞解析:你的React项目安全吗?
前端·安全·react.js
No丶slovenly2 小时前
flutter笔记-输入框
前端·笔记·flutter
国产化创客3 小时前
ESP32+Web实现智能气象站
前端·物联网·智能家居·智能硬件
coderYYY4 小时前
VSCode终端启动报错
前端·ide·vscode·npm·编辑器
tod1134 小时前
Redis 数据类型与 C++ 客户端实践指南(redis-plus-plus)
前端·c++·redis·bootstrap·html
Sylvia33.4 小时前
火星数据:棒球数据API
java·前端·人工智能
weixin199701080165 小时前
1688商品详情页前端性能优化实战
前端·性能优化
DEMO派5 小时前
前端常用XSS攻击演示与防御方案解析
前端·xss