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");
}
};