欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > el-date-picker 限制选择六个月内的日期

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

2024/10/25 10:19:09 来源:https://blog.csdn.net/qq_40797578/article/details/140287725  浏览:    关键词:el-date-picker 限制选择六个月内的日期

效果如图:在这里插入图片描述

代码:

 <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” 取消清除功能 ,按需写

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]}},
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com