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

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

此时就会出现第一次点击此刻按钮之后就无法再继续点击了

html 复制代码
<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>
javascript 复制代码
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;
 }
}

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

相关推荐
tryCbest7 小时前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js
Lanwarf-前端开发19 小时前
Vue3 + Canvas/WebGL 雷达扫描可视化实战:PPI、RHI、VOL、SVOL 四种模式原理与实现
前端·javascript·vue.js·webgl
FRing0320 小时前
vue3干货总结2(vue3生命周期、父子通信、模版引用、provide和inject)
开发语言·前端·vue.js·vue3
小码哥0681 天前
智慧物业系统源码定制:公告发布范围与住户阅读记录设计
前端·javascript·vue.js·物业管理·物业系统·物业软件
码云社区1 天前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
cc.ChenLy1 天前
无法修改 el-dialog 样式原因及解决办法
javascript·vue.js·elementui
AI开源软件1 天前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
1-1=01 天前
Vue 脚手架
前端·javascript·vue.js
一个游离的指针2 天前
Vue2:vue-router
前端·javascript·vue.js
用户4514194293222 天前
2026 Vue 生态分析:底层重写了一遍,升级却几乎零成本
vue.js