vue开始时间小于等于结束时间,且开始时间小于等于系统时间,时间格式:年月日时分
// 日期配置
export const DATA_CONFIGS = [
{
itemKey: "startDate",
startDateKey: "startDate",
endDateKey: "endDate",
isStart: true,
},
{
itemKey: "endDate",
startDateKey: "startDate",
endDateKey: "endDate",
isStart: false,
},
];
onBeforeMount(async () => {
// 遍历配置项,批量设置日期配置
DATA_CONFIGS.forEach((config) => handleDateConfig(config));
});
// 禁用日期开始时间>=结束时间,开始时间最多选到当前后台系统时间,
function handleDateConfig(config) {
const { itemKey, isStart = true } = config;
const item = findItem(formConfig.value.formItems, itemKey);
if (item) {
// 禁用日期
item.disabledDate = (date) =>
isStart
? startDateDisabled(date, form.value.endDate, sysTime.value)
: endDateDisabled(date, form.value.startDate);
// 禁用小时
item.disabledHours = () =>
hoursDisabled(
form.value.startDate || (isStart && sysTime.value),
form.value.endDate || (isStart && sysTime.value),
isStart,
);
// 禁用分钟
item.disabledMinutes = () =>
minutesDisabled(
form.value.startDate || (isStart && sysTime.value),
form.value.endDate || (isStart && sysTime.value),
isStart,
);
}
}
处理开始时间,若开始时间大于结束时间,开始时间值修改为结束时间
// 开始时间
watch(
() => form.value?.startDate,
() => {
handleStartDate(form);
},
);
// 处理开始时间,若开始时间大于结束时间,开始时间值修改为结束时间
export function handleStartDate(form) {
const { startDate } = form.value;
// 如果开始时间存在并且结束时间存在
if (startDate && form.value.endDate) {
const startDateObj = dayjs(startDate);
const endDateObj = dayjs(form.value.endDate);
// 如果开始时间大于结束时间
if (startDateObj.isAfter(endDateObj)) {
// 将开始时间修改为结束时间
form.value.startDate = endDateObj.format("YYYY-MM-DD HH:mm");
}
}
}
处理结束时间,若结束时间小于开始时间,结束时间值修改为开始时间
// 结束时间
watch(
() => form.value?.endDate,
() => {
handleEndDate(form);
},
);
// 处理结束时间,若结束时间小于开始时间,结束时间值修改为开始时间
export function handleEndDate(form) {
const { startDate } = form.value;
// 如果开始时间存在并且结束时间存在
if (startDate && form.value.endDate) {
const startDateObj = dayjs(startDate);
const endDateObj = dayjs(form.value.endDate);
// 若结束时间小于开始时间,结束时间值修改为开始时间
if (endDateObj.isBefore(startDateObj)) {
form.value.endDate = dayjs(startDate).format("YYYY-MM-DD HH:mm");
}
}
}
禁用日期方法
/**
* 禁用开始日期,即在结束日期或当前系统日期之后。
*
*/
export const startDateDisabled = (date, endDate, sysTime) => {
const _date = dayjs(date).startOf("day");
const endDateWithoutTime = dayjs(endDate).startOf("day"); // 获取没有时间部分的 endDate
const sysDateWithoutTime = dayjs(sysTime).startOf("day"); // 获取没有时间部分的 sysTime
// 只要 _date 超过其中任何一个日期(即 'endDate' 或 'sysTime'),禁用日期
return _date.isAfter(endDateWithoutTime) || _date.isAfter(sysDateWithoutTime);
};
/**
* 禁用结束日期,即在开始日期之后。
*
*/
export const endDateDisabled = (date, startDate) => {
if (!startDate || !date) return false;
const _date = dayjs(date); // 转换成 dayjs 对象,直接比较
// 获取没有时间部分的 startDate
const startDateWithoutTime = dayjs(startDate).startOf("day");
// 只要 _date 超过startDate,禁用日期
return _date.isBefore(startDateWithoutTime);
};
export function findItem(formItems, val, property = "name") {
return formItems.find((item) => item[property] === val) || {};
}
禁用小时和分钟方法
// 禁用-小时
export const hoursDisabled = (startTime, endTime, isStartTime = true) => {
if (!startTime || !endTime) {
return [];
}
if (isSameDate(startTime, endTime)) {
if (isStartTime) {
const endHour = dayjs(endTime).hour();
// 生成从 endHour + 1 到 23 的数组
return Array.from({ length: 24 - endHour - 1 }, (_, index) => endHour + 1 + index);
} else {
const startHour = dayjs(startTime).hour();
// 生成从0 到 开始小时-1 的数组
return Array.from({ length: startHour }, (v, k) => k);
}
}
return [];
};
// 禁用-分钟
export const minutesDisabled = (startTime, endTime, isStartTime = true, allowEqual = true) => {
if (!startTime || !endTime) {
return [];
}
const startHour = dayjs(startTime).hour();
const endHour = dayjs(endTime).hour();
if (isSameDate(startTime, endTime)) {
if (startHour === endHour) {
if (isStartTime) {
const endMinute = dayjs(endTime).minute();
if (allowEqual) {
// 生成从 endMinute + 1 到 59 的数组
return Array.from({ length: 60 - endMinute - 1 }, (_, index) => endMinute + 1 + index);
} else {
// 生成从 endMinute 到 59 的数组
return Array.from({ length: 60 - endMinute }, (_, index) => endMinute + index);
}
} else {
const startMinute = dayjs(startTime).minute();
if (allowEqual) {
// 生成从0 到 startMinute-1 的数组
return Array.from({ length: startMinute }, (v, k) => k);
} else {
// 生成从0 到 startMinute 的数组
return Array.from({ length: startMinute + 1 }, (v, k) => k);
}
}
}
}
return [];
};
function isSameDate(date1, date2) {
// 设置为当天的开始
const formattedDate1 = dayjs(date1).startOf("day");
const formattedDate2 = dayjs(date2).startOf("day");
return formattedDate1.isSame(formattedDate2);
}