【element-plus】DateTimePicker日期选择器,限制指定日期和时间不可选择

element-plus日期选择器,在指定日期时间前不可选择。
限制日期选择,使用disabled-date
限制小时选择,使用disabled-hours
限制分钟选择,使用disabled-minutes
限制毫秒选择,使用disabled-seconds

指定日期当天的时间有限制:
在这里插入图片描述
其他日期的时间无限制:
在这里插入图片描述

全部代码:

      <el-date-picker
          v-model="state.value"
          type="datetime"
          :default-time="new Date()"
          :disabled-date="disabledDateFn"
          :disabled-hours="disabledHours"
          :disabled-minutes="disabledMinutes"
          :disabled-seconds="disabledSeconds"
          value-format="YYYY-MM-DD HH:mm:ss"
          :placeholder="请选择" />
const state = reactibe({
   value:'',
  lastDate:'2023-10-27 12:20:30'
})

// 限制日期
const disabledDateFn = (date) => {
  if (date.getTime() < new Date(state.lastDate).getTime() - 8.64e7) {
    return true;
  }
  return false;
};

// 限制小时
const disabledHours = () => {
  const a = [];
  //选中的年月日
  const value = dayjs(state.value).format('YYYY-MM-DD');
  //指定日期的年月日
  const lastDate = dayjs(state.lastDate).format('YYYY-MM-DD');
  // 选中日期不等于指定日期,不限制
  if (value !==lastDate) {
    return;
  }
  
  for (let i = 0; i < 24; i++) {
    if (new Date(state.lastDate).getHours() > i) {
      a.push(i);
    }
  }
  return a;
};

// 限制分钟
const disabledMinutes = () => {
  const a = [];
  const value = dayjs(state.value).format('YYYY-MM-DD');
  const lastDate = dayjs(state.lastDate).format('YYYY-MM-DD');
   if (value !==lastDate) { {
    return;
  }
  for (let i = 0; i < 60; i++) {
    if (new Date(state.lastDate).getMinutes() > i) {
      a.push(i);
    }
  }
  return a;
};

// 限制毫秒
const disabledSeconds = () => {
  const a = [];
  const value = dayjs(state.value).format('YYYY-MM-DD');
  const lastDate = dayjs(state.lastDate).format('YYYY-MM-DD');
 if (value !==lastDate){
    return;
  }
  for (let i = 0; i < 60; i++) {
    if (new Date(state.lastDate).getSeconds() > i) {
      a.push(i);
    }
  }
  return a;
};

注意:选择面板上的日期后,v-model绑定的值就会更新,而不是点击确认按钮后!!!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值