el-date-picker 限制选择六个月内的日期

效果如图:

代码:

javascript 复制代码
 <el-date-picker v-model="serchTimes" type="daterange" size="small" start-placeholder="开始时间"
                                range-separator="~" end-placeholder="结束时间" format="yyyy / MM / dd "
                                :picker-options="pickerOptions" value-format="yyyy-MM-dd HH:mm:ss" @change="changeTime"
                                :default-time="['00:00:00', '23:59:59']"  :unlink-panels="true" :validate-event="false" :clearable="false">
                            </el-date-picker>

主要::picker-options="pickerOptions" 中的配置:

onPick配置项中可以获取到点击时的时间,将其转换为时间戳后存储起来。

disabledDate配置项中配置只能选择前后6个月,这个配置项的参数是当前的日期,返回参数要求是Boolean值

其他属性解析:format="yyyy / MM / dd ": 选择后显示的样式

default-time 是因为需要默认的时分秒才添加的

value-format 是点击确认后,change事件中能获取到的数据格式,

:unlink-panels="true" 是取消两个面板之间的联动滚动

:validate-event="false" 取消点击时的校验,按需写

:clearable="false" 取消清除功能 ,按需写

javascript 复制代码
data(){
    return{
        serchTimes: '',
        selectData: '',
        pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                    this.selectData = maxDate ? maxDate.getTime() : minDate ? minDate.getTime() : ''
                     if (!maxDate || !minDate) {
                        this.serchTimes = ''  //只选一个的时候,日期置空
                    }
                },
                },
                disabledDate: (time) => {
                    if (this.selectData) {
                        const curDate = this.selectData;
                        const three = 183 * 24 * 3600 * 1000;// 6个月
                        const threeMonthsAfter = curDate + three; // 开始时间+6个月
                        const threeMonthsBefore = curDate - three; //开始时间-6个月
                        return time.getTime() > threeMonthsAfter || time.getTime() < threeMonthsBefore;
                    }
                }
        },
    }
},
methods:{
    //选择时间后的处理函数了,可以把数据保存后发送接口等等操作
     changeTime() {
            if (this.serchTimes) {
                this.reloadForm.begAbsTime = this.serchTimes[0]
                this.reloadForm.endAbsTime = this.serchTimes[1]
            }
        },
}
相关推荐
90后的晨仔15 小时前
Vue 3 组合式函数(Composables)全面解析:从原理到实战
前端·vue.js
今天头发还在吗15 小时前
【React】TimePicker进阶:解决开始时间可大于结束时间的业务场景与禁止自动排版
javascript·react.js·ant design
今天头发还在吗15 小时前
【React】动态SVG连接线实现:图片与按钮的可视化映射
前端·javascript·react.js·typescript·前端框架
小刘不知道叫啥15 小时前
React 源码揭秘 | suspense 和 unwind流程
前端·javascript·react.js
90后的晨仔16 小时前
Vue 3 中 Provide / Inject 在异步时不起作用原因分析(二)?
前端·vue.js
90后的晨仔16 小时前
Vue 3 中 Provide / Inject 在异步时不起作用原因分析(一)?
前端·vue.js
90后的晨仔16 小时前
Vue 异步组件(defineAsyncComponent)全指南:写给新手的小白实战笔记
前端·vue.js
木易 士心16 小时前
Vue 与 React 深度对比:底层原理、开发体验与实际性能
前端·javascript·vue.js
Lucky GGBond17 小时前
Vue + Spring Boot 实现 Excel 导出实例
vue.js·spring boot·excel
wyzqhhhh17 小时前
插槽vue/react
javascript·vue.js·react.js