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);
}
相关推荐
芒果1255 分钟前
【转载文章】ECharts-GL 实现世界级、国家级、省市级 3D 地图
前端
一只小风华~15 分钟前
JavaScript:数组常用操作方法的总结表格
前端·javascript·数据结构·vue.js·算法
前端老鹰20 分钟前
JavaScript Array.prototype.some ():数组判断的 “快捷侦探”
前端·javascript
张元清21 分钟前
揭秘JS事件循环:一道字节跳动面试题带你深入理解async/await、Promise与RAF
前端·react.js·面试
KenXu24 分钟前
F2C-Chrome插件-Figma免费的DevMode来了!
前端
北海几经夏30 分钟前
React组件中的this指向问题
前端·react.js
程序媛李李李李李蕾36 分钟前
你不能直接用现成的吗?整个前端做笔记管理工具真是折腾人
javascript·vue.js·后端
passer98143 分钟前
列表项切换时同步到可视区域
前端
FogLetter44 分钟前
移动端适配的终极奥义:从lib-flexible到postcss-pxtorem的全方位指南
前端·postcss
易元1 小时前
设计模式-访问者模式
前端·后端·设计模式