Date TimePicker 时间选择器精确限制到时分秒,此刻按钮点击失效处理

今天在开发的时候遇到一个需求,日期时间选择器组件不能选择已经过去的年月日时分秒。用户只能选择当前时间的时间,如果年月日选择是当天之前的时间,时分秒不做限制,如果年月日选择的是当天时间,就要判断时分秒,只能选择当前时间时分秒之前的时间。

此时就会出现第一次点击此刻按钮之后就无法再继续点击了
在这里插入图片描述

<el-date-picker
   v-model="formJson.formValue.ReceivingTime"
   type="datetime"
   placeholder="请选择收款时间"
   value-format="yyyy-MM-dd HH:mm:ss"
   :picker-options="pickerOptions"
   @focus="focusReceivingTime"
 ></el-date-picker>
pickerOptions: {
  disabledDate(time) {
    return time.getTime() > Date.now(); // 禁用超过当前时间的日期
  },
  selectableRange: '00:00:00 - 23:59:59' //这个加上之后,时分秒上面才有禁止选择变灰,如果不加,也可以禁止选择,但是不会变灰
}

// 监听刚开始的数据变化处理
watch: {
  'formJson.formValue.ReceivingTime': {
    handler(newValue) {
      if (newValue) {
        let date = new Date();
        // let min = date.getMinutes();
        // date.setMinutes(min + 1);
        let nowDate = dayjs(date).format('HH:mm:ss');
        let st = '';
        if (dayjs(date).format('yyyy-MM-DD') === dayjs(newValue).format('yyyy-MM-DD')) {
          let hh1 = dayjs(newValue).format('HH:mm:ss');
          if (hh1 > nowDate) {
            this.formJson.formValue.ReceivingTime = new Date();
          }
          st = nowDate;
        } else {
          st = '23:59:59';
        }
        this.pickerOptions.selectableRange = '00:00:00 - ' + st;
      }
    },
    deep: true,
    immediate: true
  }
}


methods: {
// 获取焦点事件来处理选择时间范围
 focusReceivingTime() {
   if (!this.formJson.formValue.ReceivingTime) return;
   let newValue = JSON.parse(JSON.stringify(this.formJson.formValue.ReceivingTime));
   let date = new Date();
   let min = date.getSeconds();
   date.setSeconds(min + 20);
   let nowDate = dayjs(date).format('HH:mm:ss');
   let st = '';
   if (dayjs(date).format('yyyy-MM-DD') === dayjs(newValue).format('yyyy-MM-DD')) {
     let hh1 = dayjs(newValue).format('HH:mm:ss');
     if (hh1 > nowDate) {
       this.formJson.formValue.ReceivingTime = new Date();
     }
     st = nowDate;
   } else {
     st = '23:59:59';
   }
   this.pickerOptions.selectableRange = '00:00:00 - ' + st;
 }
}

如果这篇文章对你有所帮助,欢迎点赞、分享和留言,让更多的人受益。感谢你的细心阅读,如果你发现了任何错误或需要补充的地方,请随时告诉我,我会尽快处理。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:06:08       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:06:08       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:06:08       58 阅读
  4. Python语言-面向对象

    2024-07-09 21:06:08       69 阅读

热门阅读

  1. 服务发现与注册:Eureka与Consul

    2024-07-09 21:06:08       20 阅读
  2. Vite 中怎么添加全局 scss 文件

    2024-07-09 21:06:08       25 阅读
  3. 深入理解 CSS 选择器:全面指南

    2024-07-09 21:06:08       24 阅读
  4. 记一次使用“try-with-resources“的语法导致的BUG

    2024-07-09 21:06:08       28 阅读
  5. area_center 区域和区域中心。

    2024-07-09 21:06:08       31 阅读
  6. Linux

    2024-07-09 21:06:08       23 阅读
  7. 从vs中删除自带的Microsoft Git Provider

    2024-07-09 21:06:08       17 阅读
  8. 设计模式的一点理解

    2024-07-09 21:06:08       18 阅读
  9. QT 设置控件的展开和消失

    2024-07-09 21:06:08       22 阅读
  10. qt 读取配置文件

    2024-07-09 21:06:08       21 阅读