vue开始时间小于结束时间,时间格式:年月日时分

bash 复制代码
// 1次装车配置和2次装车配置的日期
export const DATA_CONFIGS = [
  {
    itemKey: "inDate",
    startDateKey: "inDate",
    endDateKey: "outDate",
    isStart: true,
    isFirstShip: true,
  },
  {
    itemKey: "outDate",
    startDateKey: "inDate",
    endDateKey: "outDate",
    isStart: false,
    isFirstShip: true,
  },
  {
    itemKey: "inDate",
    startDateKey: "inDate",
    endDateKey: "outDate",
    isStart: true,
    isFirstShip: false,
  },
  {
    itemKey: "outDate",
    startDateKey: "inDate",
    endDateKey: "outDate",
    isStart: false,
    isFirstShip: false,
  },
];
bash 复制代码
onBeforeMount(() => {
    // 遍历配置项,批量设置日期配置
    DATA_CONFIGS.forEach((config) => handleDateConfig(config));
  });
bash 复制代码
function handleDateConfig(config) {
    const { itemKey, startDateKey, endDateKey, isStart = true, isFirstShip = true } = config;
    if (itemKey === "inDate" || itemKey === "outDate") {
      // 是1次装车日期禁用还是2次装车日期禁用
      const formItems = isFirstShip
        ? firstShipConfig.value.formItems
        : secondShipConfig.value.formItems;
      const item = findItem(formItems, itemKey);
      const dataIndex = isFirstShip ? 0 : 1;
      if (item) {
        // 禁用日期
        item.disabledDate = (date) =>
          isStart
            ? startDateDisabled(date, form.value.carCountList[dataIndex][endDateKey])
            : endDateDisabled(date, form.value.carCountList[dataIndex][startDateKey]);
        // 禁用小时
        item.disabledHours = () =>
          hoursDisabled(
            form.value.carCountList[dataIndex][startDateKey],
            form.value.carCountList[dataIndex][endDateKey],
            isStart,
          );
        // 禁用分钟
        item.disabledMinutes = () =>
          minutesDisabled(
            form.value.carCountList[dataIndex][startDateKey],
            form.value.carCountList[dataIndex][endDateKey],
            isStart,
            false,
          );
      }
    }
  }

处理开始时间,若开始时间大于结束时间,开始时间值修改为结束时间-1分钟

bash 复制代码
// 开始时间
  watch(
    () => form.value?.carCountList?.[0]?.inDate,
    () => {
      handleStartDate(0, form);
    },
  );
  watch(
    () => form.value?.carCountList?.[1]?.inDate,
    () => {
      handleStartDate(1, form);
    },
  );
	// 处理开始时间,,若开始时间大于结束时间,开始时间值修改为结束时间-1分钟
export function handleStartDate(index, form) {
  if (!form.value?.carCountList?.length) return;

  const { inDate } = form.value.carCountList[index];

  // 如果开始时间存在并且结束时间存在
  if (inDate && form.value.carCountList[index].outDate) {
    const startDateObj = dayjs(inDate);
    const endDateObj = dayjs(form.value.carCountList[index].outDate);

    // 如果开始时间大于或等于结束时间
    if (startDateObj.isAfter(endDateObj) || startDateObj.isSame(endDateObj)) {
      // 将开始时间修改为结束时间减去1分钟
      form.value.carCountList[index].inDate = endDateObj
        .subtract(1, "minute")
        .format("YYYY-MM-DD HH:mm");
    }
  }
}

处理结束时间,若结束时间小于开始时间,结束时间值修改为开始时间+1分钟

bash 复制代码
  // 出站时间
  watch(
    () => form.value?.carCountList?.[0]?.outDate,
    (n) => {
      handleOutDate(0, n, form, firstShipConfig, formRefs);
    },
  );
  watch(
    () => form.value?.carCountList?.[1]?.outDate,
    (n) => {
      handleOutDate(1, n, form, secondShipConfig, formRefs);
    },
  );
  // 处理结束时间,若结束时间小于开始时间,结束时间值修改为开始时间+1分钟
export function handleEndDate(index, form) {
  if (!form.value?.carCountList?.length) return;

  const { inDate: startDate } = form.value.carCountList[index];

  // 如果开始时间存在
  if (startDate) {
    const startDateFormatted = dayjs(startDate).format("YYYY-MM-DD");
    const endDateFormatted = dayjs(form.value.carCountList[index].outDate).format("YYYY-MM-DD");

    if (
      !dayjs(startDate).isBefore(dayjs(form.value.carCountList[index].outDate)) &&
      startDateFormatted === endDateFormatted
    ) {
      form.value.carCountList[index].outDate = dayjs(startDate)
        .add(1, "minute")
        .format("YYYY-MM-DD HH:mm");
    }
  }
}
bash 复制代码
/**
 * 检查日期是否在结束日期之后,或者根据特定的结束日期时间部分进行调整。
 *
 * 如果结束日期的时间部分是 "00:00",则日期的禁用时间会减去1天。
 *
 */
export const startDateDisabled = (date, endDate) => {
  if (!endDate || !date) return false;

  const _date = dayjs(date); // 转换成 dayjs 对象,直接比较

  // 获取 endDate 的时间部分
  const time = dayjs(endDate).format("HH:mm");

  // 判断 endDate 的时间部分是否是 "00:00"
  const endDateWithoutTime = dayjs(endDate).startOf("day"); // 获取没有时间部分的 endDate
  const adjustedEndDate =
    time === "00:00" ? endDateWithoutTime.subtract(1, "day") : endDateWithoutTime;

  // 比较日期
  return _date.isAfter(adjustedEndDate);
};
/**
 * 检查日期是否在开始日期之前,或者根据特定的开始日期时间部分进行调整。
 *
 * 如果开始日期的时间部分是 "23:59",则日期的禁用时间会加1天。
 *
 */
export const endDateDisabled = (date, startDate) => {
  if (!startDate || !date) return false;

  const _date = dayjs(date); // 转换成 dayjs 对象,直接比较
  // 获取 startDate 的时间部分
  const time = dayjs(startDate).format("HH:mm");

  // 判断 startDate 的时间部分是否是 "23:59"
  const startDateWithoutTime = dayjs(startDate).startOf("day"); // 获取没有时间部分的 endDate

  const adjustedEndDate =
    time === "23:59" ? startDateWithoutTime.add(1, "day") : startDateWithoutTime;
  // 比较日期
  return _date.isBefore(adjustedEndDate);
};
bash 复制代码
// 禁用-小时
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 [];
};
// 禁用-秒数
export const secondsDisabled = (startTime, endTime, isStartTime = true) => {
  if (!startTime || !endTime) {
    return [];
  }

  const startHour = dayjs(startTime).hour();
  const endHour = dayjs(endTime).hour();
  const startMinute = dayjs(startTime).minute();
  const endMinute = dayjs(endTime).minute();

  if (isSameDate(startTime, endTime)) {
    if (startHour === endHour && startMinute === endMinute) {
      if (isStartTime) {
        const endSecond = dayjs(endTime).second();
        // 生成从 endSecond + 1 到 59 的数组
        return Array.from({ length: 60 - endSecond - 1 }, (_, index) => endSecond + 1 + index);
      } else {
        const startSecond = dayjs(startTime).second();
        // 生成从0 到 startSecond-1 的数组
        return Array.from({ length: startSecond }, (v, k) => k);
      }
    }
  }
  return [];
};
function isSameDate(date1, date2) {
  // 设置为当天的开始
  const formattedDate1 = dayjs(date1).startOf("day");
  const formattedDate2 = dayjs(date2).startOf("day");
  return formattedDate1.isSame(formattedDate2);
}
相关推荐
Java小卷23 分钟前
流程设计器为啥选择diagram-js
前端·低代码·工作流引擎
HelloReader1 小时前
Isolation Pattern(隔离模式)在前端与 Core 之间加一道“加密网关”,拦截与校验所有 IPC
前端
兆子龙1 小时前
从 float 到 Flex/Grid:CSS 左右布局简史与「刁钻」布局怎么搞
前端·架构
YukiMori231 小时前
一个有趣的原型继承实验:为什么“男人也会生孩子”?从对象赋值到构造函数继承的完整推演
前端·javascript
_哆啦A梦2 小时前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
百里静修2 小时前
一个 Hook 拦截所有 AJAX 请求:ajax-hooker 使用指南与原理
前端
摸鱼的春哥2 小时前
惊!黑客靠AI把墨西哥政府打穿了,海量数据被黑
前端·javascript·后端
小兵张健2 小时前
Playwright MCP 截图标注方案调研(推荐方案1)
前端·javascript·github
小兵张健3 小时前
AI 页面与交互迁移流程参考
前端·ai编程·mcp
小兵张健4 小时前
掘金发布 SOP(Codex + Playwright MCP + Edge)
前端·mcp