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]
            }
        },
}
相关推荐
用户9385156350714 分钟前
手写一个 LLM Harness 框架:用工程化手段把大模型幻觉踩在脚下
javascript·人工智能·后端
油丶酸萝卜别吃1 小时前
jquery-ajax.js 说明文档
前端·javascript·jquery
windliang1 小时前
Claude Code 源码分析(九):子 Agent 如何分叉、继续与回到父会话
前端·javascript·面试
柒和远方2 小时前
V063: TS 面试必考:interface 与 type 的四大差异,与 LLM Harness 的自动化择优
前端·javascript
宿6742 小时前
vue3-env环境
前端·vue.js
汉堡大王95272 小时前
Vue 3 + TS + Element Plus 实战:如何从零搭建企业级违章记录管理 SaaS 前端
前端·javascript·vue.js
海天鹰2 小时前
屏幕颜色检测
javascript·html5
用户7917286761973 小时前
opencode-plugin-peers完全指南:OpenCode如何实现 Claude Code式跨会话消息
javascript
andr_gale5 小时前
02_uniapp自定义上凸效果的底部TabBar
前端·javascript·uni-app·移动端
倾听醉梦语6 小时前
React/Vite/Next.js 前端开发工具 SpotPatch:点击页面元素精准定位 JSX/TSX 源码
javascript·react·ai编程·vite·next.js·前端开发工具