el-datepicker禁用未来日期(包含时分秒)type=‘datetime’

文章目录

    • 实现代码

      • 方式1:当选中日期的时候去更新一次。
      • [方式2: 优化版本,使用 setTimout 每分钟更新一次。(防止选中日期之后过了很久再去选择时分秒时没有根据当前时间去改变)](#方式2: 优化版本,使用 setTimout 每分钟更新一次。(防止选中日期之后过了很久再去选择时分秒时没有根据当前时间去改变))
    • 2024年10月30日更新:

      • [禁用日期后 此刻按钮功能失效解决办法:](#禁用日期后 此刻按钮功能失效解决办法:)

      el-datepicker 选择器禁用未来日期,动态禁用 时分秒;
      秒 这一列的选项没有禁用的样式 可以随便选,
      但是 点击确定后时间会更新到限制的那个时间(选中日期的那一刻)。

实现代码

方式1:当选中日期的时候去更新一次。

html 复制代码
<template>
  <div class="hello">
    <el-date-picker
      v-model="time"
      type="datetime"
      :picker-options="{
        disabledDate(time) {
          const nowTime = new Date()
          return new Date(time).getTime() > nowTime.getTime()
        },
        selectableRange
      }"
    ></el-date-picker>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',

  data() {
    return {
      time: ''
    }
  },

  computed: {
  	// 限制时分秒选择范围
  	/*
  	* 当切换年月日的时候要动态去改变
  	* onPick 方法只能在 时间范围 选择器才能用;
  	* 但当修改选中日期的时候可以使用 watch / computed 监听到变动。
  	*/
    selectableRange() {
      const nowTime = new Date()
      const isSame = new Date(this.time).toLocaleDateString() === nowTime.toLocaleDateString()
      if (isSame) {
        return `00:00:00-${nowTime.getHours()}:${nowTime.getMinutes()}:${nowTime.getSeconds()}`
      }
      return '00:00:00-23:59:59'
    }
  }
}
</script>

方式2: 优化版本,使用 setTimout 每分钟更新一次。(防止选中日期之后过了很久再去选择时分秒时没有根据当前时间去改变)

html 复制代码
<template>
  <div class="hello">
    <el-date-picker
      v-model="time"
      type="datetime"
      :picker-options="{
        disabledDate(time) {
          const nowTime = new Date()
          return new Date(time).getTime() > nowTime.getTime()
        },
        selectableRange
      }"
      @change="changeDate"
    ></el-date-picker>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',

  data() {
    return {
      time: '',
      selectableRange: '00:00:00-23:59:59',
      timer: null
    }
  },

  watch: {
    time(newTime, oldTime) {
      // 在滚动选择时分秒的时候也会被 watch 监听到
      // 在这里判断 如果年月日相同,就不再去更新。
      const sameDay = new Date(newTime).toLocaleDateString() === new Date(oldTime).toLocaleDateString()
      if (sameDay) return
      this.updateSelectableRange()
    }
  },

  methods: {
    updateSelectableRange() {
      const nowTime = new Date()
      const isSame = new Date(this.time).toLocaleDateString() === nowTime.toLocaleDateString()
      this.clearTimer()

      if (isSame) {
        this.selectableRange = `00:00:00-${nowTime.getHours()}:${nowTime.getMinutes()}:${nowTime.getSeconds()}`

        // 创建一个定时器,每分钟更新去更新一次禁用范围。
        const delay = 60 - nowTime.getSeconds()
        this.timer = setTimeout(() => {
          this.updateSelectableRange()
        }, delay * 1000)
        return
      }
      this.selectableRange = '00:00:00-23:59:59'
    },

    clearTimer() {
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
    },

    changeDate() {
      // 选中日期之后清除掉定时器
      this.clearTimer()
    }
  }
}
</script>

2024年10月30日更新:

禁用日期后 此刻按钮功能失效解决办法:

el-datepicker此刻按钮点击失效

相关推荐
龙飞05几秒前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、6 分钟前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao6 分钟前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly12 分钟前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
hedley(●'◡'●)42 分钟前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
Cult Of42 分钟前
Alicea Wind的个人网站开发日志(2)
开发语言·python·vue
qq5_81151751543 分钟前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
百思可瑞教育44 分钟前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
百锦再44 分钟前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架
CappuccinoRose1 小时前
JavaScript 学习文档(二)
前端·javascript·学习·数据类型·运算符·箭头函数·变量声明