el-date-picker,日期时间范围选择器默认只能选择7天

elementplus组件

html 复制代码
<el-date-picker
	style="width: 100%"
	v-model="dayTime"
	type="datetimerange"
	:start-placeholder="$t('public.startTime')"
	:end-placeholder="$t('public.endTime')"
	@change="changeEndDate"
	@calendar-change="calendarChange"
	format="YYYY/MM/DD HH:mm:ss"
	:disabled-date="disabledDateOption"
	:clearable="false"
/>

dayTime:用户选择的时间范围数组

disableDate:根据选择的第一个时间点来判断用户选择的前后7天的时间结束点

TypeScript 复制代码
const dayTime = ref<Array<string>>([]);
const disableDate = ref<string>("");

calendarChange事件,用户选择完第二个时间值后把之前用来判断的disableDate置空

TypeScript 复制代码
const calendarChange = (time: any[]) => {
	disableDate.value = time[0];
	if (time[1]) {
		disableDate.value = "";
	}
};

disabledDateOption:判断禁用的时间范围

TypeScript 复制代码
const disabledDateOption = (time: any) => {
	if (disableDate.value) {
		disableDate.value = moment(new Date(disableDate.value)).format("YYYY-MM-DD HH:mm:ss");
		return (
			moment(time).isAfter(moment(disableDate.value).add(7, "days"), "days") ||
			moment(time).isBefore(moment(disableDate.value).subtract(7, "days"), "days") ||
			moment().isBefore(time, "days")
		);
	} else {
		return moment().isBefore(time, "days");
	}
};
相关推荐
小李子呢02115 小时前
前端八股CSS(2)---动画的实现方式
前端·javascript
GreenTea7 小时前
从 Claw-Code 看 AI 驱动的大型项目开发:2 人 + 10 个自治 Agent 如何产出 48K 行 Rust 代码
前端·人工智能·后端
渣渣xiong8 小时前
从零开始:前端转型AI agent直到就业第五天-第十一天
前端·人工智能
布局呆星8 小时前
Vue3 | 组件通信学习小结
前端·vue.js
C澒8 小时前
IntelliPro 企业级产研协作平台:前端智能生产模块设计与落地
前端·ai编程
OpenTiny社区9 小时前
重磅预告|OpenTiny 亮相 QCon 北京,共话生成式 UI 最新技术思考
前端·开源·ai编程
前端老实人灬9 小时前
web前端面试题
前端
Moment9 小时前
AI 全栈指南:NestJs 中的 Service Provider 和 Module
前端·后端·面试
IT_陈寒9 小时前
为什么我的JavaScript异步回调总是乱序执行?
前端·人工智能·后端