一个基于vue3+javascript+elementPlus+dayjs 实现 日期时间选择器(datetimerange)并禁用未来时间(到时分秒)

一个基于vue3+javascript+elementPlus+dayjs 实现 日期时间选择器并禁用未来时间(到时分秒)

一定要注意elementPlus版本问题 ,十分重要!!!

本地版本是"element-plus": “^2.10.3”
话不多说,直接上代码


  <template><div class="demo-datetime-picker">
    <div class="block">
      <span class="demonstration"
        >日期时间-type:datetimerange 禁止选择未来时间</span
      >
      <el-date-picker
        v-model="datetimerange"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :disabled-date="disabledDate"
        :disabled-hours="disabledHours"
        :disabled-minutes="disabledMinutes"
        :disabled-seconds="disabledSeconds"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from "vue";
import dayjs from "dayjs";
const now = computed(() => {
  return dayjs();
});
const datetimerange = ref([]);
// 获取当前角色对应的时间值
const getTargetTime = (role) => {
  return role === "start" ? datetimerange.value[0] : datetimerange.value[1];
};
// 检查是否为当天
const isToday = (role) => {
  const targetValue = getTargetTime(role);
  return targetValue ? dayjs(targetValue).isSame(dayjs(), "day") : false;
};
// 检查是否为当前小时
const isCurrentHour = (hour, role) => {
  const targetValue = getTargetTime(role);
  return targetValue
    ? dayjs(targetValue).hour() === hour &&
        dayjs(targetValue).hour() === dayjs().hour()
    : false;
};

// 禁止选择未来日期
const disabledDate = (time) => {
  return dayjs(time).isAfter(dayjs().endOf("day"));
};

// 禁止选择未来小时
const disabledHours = (role) => {
  if (!isToday(role)) return [];
  const currentHour = now.value.hour();
  return Array.from(
    { length: 23 - currentHour },
    (_, i) => currentHour + 1 + i
  );
};
// 禁止选择当前小时后的分钟
const disabledMinutes = (selectedHour, role) => {
  // 不是当天或不是当前小时,不禁用分钟
  if (!isToday(role) || !isCurrentHour(selectedHour, role)) return [];
  const currentMinute = now.value.minute();
  return Array.from(
    { length: 59 - currentMinute },
    (_, i) => currentMinute + 1 + i
  );
};

// 禁止选择当前分钟后的秒
const disabledSeconds = (selectedHour, selectedMinute, role) => {
  if (!isToday(role) || !isCurrentHour(selectedHour, role)) return [];
  if (selectedMinute !== now.value.minute()) return [];
  const currentSecond = now.value.second();
  return Array.from(
    { length: 59 - currentSecond },
    (_, i) => currentSecond + 1 + i
  );
};
</script>

<style scoped>
.block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}
.block:last-child {
  border-right: none;
}
.block .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
</style>


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值