el-date-picker的type设为datetimerange,即日期时间段,限制到时分秒

// 不可选日期时间
const disabledDate = (time: Date) => {
  return time.getTime() < Date.now() - 8.64e7;
};
const selectedTimeRange = ref({ start: null, end: null }); // 用于存储开始和结束时间
const handleCalendarChange = (val: [Date, null | Date]) => {
  selectedTimeRange.value = {
    start: val[0],
    end: val[1]
  };
};
const disabledHours = (type: string) => {
  const now = new Date();
  if (!selectedTimeRange.value.start && !selectedTimeRange.value.end) {
    // 如果尚未选择任何时间,返回空数组
    return [];
  }
  const selectedDate = type === 'start' ? selectedTimeRange.value.start : selectedTimeRange.value.end;

  if (selectedDate) {
    const selectedDay = selectedDate.getDate();
    const currentDay = now.getDate();
    // 如果选择的是今天,则禁用当前小时之前的小时
    if (selectedDay === currentDay) {
      return Array.from({ length: now.getHours() }, (_, i) => i);
    }
  }
  return [];
};
const disabledMinutes = (hour: number, type: string) => {
  const now = new Date();
  const selectedDate = type === 'start' ? selectedTimeRange.value.start : selectedTimeRange.value.end;

  if (selectedDate) {
    const selectedDateTime = new Date(selectedDate);
    const isToday = selectedDateTime.getDate() === now.getDate();
    const isCurrentHour = hour === now.getHours();

    // 如果选择的日期是今天,且小时是当前小时,禁用当前分钟之前的分钟
    if (isToday && isCurrentHour) {
      return Array.from({ length: now.getMinutes() }, (_, i) => i);
    }
  }
  return [];
};

const disabledSeconds = (hour: number, minute: number, type: string) => {
  const now = new Date();
  const selectedDate = type === 'start' ? selectedTimeRange.value.start : selectedTimeRange.value.end;

  if (selectedDate) {
    const selectedDateTime = new Date(selectedDate);
    const isToday = selectedDateTime.getDate() === now.getDate();
    const isCurrentHour = hour === now.getHours();
    const isCurrentMinute = minute === now.getMinutes();

    // 如果选择的日期、小时和分钟是当前时间,则禁用当前秒之前的秒
    if (isToday && isCurrentHour && isCurrentMinute) {
      return Array.from({ length: now.getSeconds() }, (_, i) => i);
    }
  }
  return [];
};


 <el-date-picker
              v-model="disposableDateTime"
              type="datetimerange"
              range-separator="至"
              :disabled-hours="disabledHours"
              :disabled-minutes="disabledMinutes"
              :disabled-seconds="disabledSeconds"
              value-format="YYYY-MM-DD HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @calendar-change="handleCalendarChange"
              :disabled-date="disabledDate"
            />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值