当前位置: 首页 > news >正文

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 + 123 的数组
      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 + 159 的数组
          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);
}
http://www.dtcms.com/a/108262.html

相关文章:

  • Python每日一题(13)
  • 【算法进阶详解】线段树应用
  • 洛谷题单2-P2433 【深基1-2】小学数学 N 合一-python-流程图重构
  • 脑影像分析软件推荐 | CONN
  • Django接入 免费的 AI 大模型——讯飞星火(2025年4月最新!!!)
  • 安装完 miniconda3 ,cmd无法执行 conda 命令
  • 接口测试(2)
  • PyTorch 深度学习实战(32):多模态学习与CLIP模型
  • 中级:Spring框架面试题全解析
  • Labview信号采集与多功能分析系统(可仿真)
  • Python基于Django的新生入学管理系统(附源码,文档说明)
  • 06-01-自考数据结构(20331)- 查找技术-静态查找
  • 【Linux系统篇】:Linux文件管理的“地图与指南针”--从文件描述符表到内核缓冲区
  • IDEA的基础快捷键
  • centos7强制升级docker
  • jupyter notebook笔记:下拉菜单中添加新的conda 环境
  • 人工智能在生物医药-新版ChatGPT-4o辅助一键生成机制图
  • 实战 | 餐厅点餐小程序技术解析:SpringBoot + UniApp 高效开发指南
  • c++柔性数组、友元、类模版
  • ubuntu18 server版花屏问题
  • 脊椎CT图像分割技术详解
  • python中的 f 是什么意思,f‘{username}_log_archive_{int(time.time())}.txt‘
  • 【diffusers 进阶(十四)】权重读取,查看 Lora 具体加在哪里和 Rank ‘秩’ 是多少?以 OminiControl 为例
  • Vue3+Vite+TypeScript+Element Plus开发-03.主页设计与router配置
  • 智能设备运行监控系统
  • intellij Idea 和 dataGrip下载和安装教程
  • 【Nature正刊2023】使用大型语言模型进行自主化学研究
  • 解决小程序video控件在真机和上线后黑屏不播放问题
  • 【ESP32-IDF 笔记】04-I2C配置
  • Scala基础知识5