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]
            }
        },
}
相关推荐
listhi5208 分钟前
Map对象在JavaScript循环中的使用
开发语言·前端·javascript
小熊学Java9 分钟前
基于 Spring Boot+Vue 的高校竞赛管理平台
vue.js·spring boot·后端
百思可瑞教育4 小时前
uni-app 根据用户不同身份显示不同的tabBar
vue.js·uni-app·北京百思可瑞教育·北京百思教育
雾恋8 小时前
最近一年的感悟
前端·javascript·程序员
华仔啊8 小时前
Vue3 的 ref 和 reactive 到底用哪个?90% 的开发者都选错了
javascript·vue.js
A黄俊辉A9 小时前
axios+ts封装
开发语言·前端·javascript
小李小李不讲道理9 小时前
「Ant Design 组件库探索」四:Input组件
前端·javascript·react.js
郑板桥3010 小时前
tua-body-scroll-lock踩坑记录
前端·javascript
IT古董10 小时前
Vue + Vite + Element UI 实现动态主题切换:基于 :root + SCSS 变量的最佳实践
vue.js·ui·scss
解道Jdon10 小时前
SpringBoot4与Spring7发布:云原生深度进化
javascript·reactjs