当前位置: 首页 > news >正文

日期时间选择(设置禁用状态)

目录

1.element文档需要

2.禁用所有过去的时间

3.设置指定日期的禁用时间


<template><div class="block"><span class="demonstration">起始日期时刻为 12:00:00</span><el-date-pickerv-model="value1"type="datetimerange"start-placeholder="开始日期"end-placeholder="结束日期":default-time="['12:00:00']"></el-date-picker></div>
</template><script>export default {data() {return {value1: ''};}};
</script>

1.element文档需要

Element - The world's most popular Vue UI framework

picker-options

<template><div class="block"><span class="demonstration">起始日期时刻为 12:00:00</span><el-date-pickerv-model="value1"type="datetimerange"start-placeholder="开始日期"end-placeholder="结束日期":picker-options="pickerOptions"></el-date-picker></div>
</template><script>export default {data() {return {value1: ''};},};
</script>

 :picker-options="pickerOptions"

2.禁用所有过去的时间

 computed:{pickerOptions(){return {disabledDate:this.disabledDate()}}},
methods:{disabledDate(time){console.log(time)return time<Date.now()}
}

disabledDate 是一个函数,用于判断某个日期是否应该被禁用。这个函数会在日期选择器中每个日期渲染时被调用,传入当前日期的 timestamp(时间戳),返回一个布尔值,表示该日期是否被禁用。

Date.now() 返回当前时间的时间戳(即从1970年1月1日00:00:00 UTC开始经过的毫秒数)

time<Date.now()  表示如果传入的 time (选中的日期时间戳)小于当前时间的时间戳,则会返回 true,表示该日期应该被禁用

换句话说,这个逻辑会禁用所有过去的时间

3.设置指定日期的禁用时间

假如现在有个指定日期选择器

<template><div class="block"><span class="demonstration">默认</span><el-date-pickerv-model="time"type="date"value-format="timestamp"placeholder="选择日期"></el-date-picker></div>
</template><script>export default {data() {return {time: '',};}};
</script>

此时data中的time就是需要的指定日期

value-format="timestamp"返回时间戳

computed:{pickerOptions(){return {disabledDate:this.disabledDate()}}},
 disabledDate(time) {let allowedDate = new Date(this.time);console.log('allowedDate',allowedDate);      let startOfDay = new Date(allowedDate.getFullYear(), allowedDate.getMonth(), allowedDate.getDate());let endOfDay = new Date(allowedDate.getFullYear(), allowedDate.getMonth(), allowedDate.getDate() + 1);return time.getTime() < startOfDay.getTime() || time.getTime() >= endOfDay.getTime();},

打印结果;

完整代码:

<template><div class="block"><el-date-pickerv-model="time"type="date"value-format="timestamp"placeholder="选择日期"></el-date-picker>//设置禁用时间<el-date-pickerv-model="value1"type="datetimerange"start-placeholder="开始日期"end-placeholder="结束日期":picker-options="pickerOptions"></el-date-picker></div>
</template><script>export default {computed:{pickerOptions(){return {disabledDate:this.disabledDate()}}},data() {return {value1: '',time:'',};},methods:{disabledDate(time) {let allowedDate = new Date(this.time);   let startOfDay = new Date(allowedDate.getFullYear(), allowedDate.getMonth(), allowedDate.getDate());let endOfDay = new Date(allowedDate.getFullYear(), allowedDate.getMonth(), allowedDate.getDate() + 1);return time.getTime() < startOfDay.getTime() || time.getTime() >= endOfDay.getTime();},}};
</script>

展示

http://www.lryc.cn/news/513960.html

相关文章:

  • 基于SpringBoot的题库管理系统的设计与实现(源码+SQL+LW+部署讲解)
  • 钉钉h5微应用安卓报错error29 ios报错error3 加上报错52013,签名校验失败 (前端)
  • Vue.js组件开发-客户端如何限制刷新Token次数
  • Linux上安装jdk
  • Ardunio BLE keyboard 库的使用
  • django --递归查询评论
  • 【开源免费】基于SpringBoot+Vue.JS音乐网站(JAVA毕业设计)
  • SUBSTRING_INDEX()在MySQL中的用法
  • 对45家“AI+安全”产品/方案的分析
  • Oracle Dataguard(主库为 Oracle 11g 单节点)配置详解(1):Oracle Dataguard 概述
  • Pycharm 中 virtualenv、pipenv、conda 虚拟环境的用法
  • UNI-APP弹窗
  • 【大模型实战篇】LLaMA Factory微调ChatGLM-4-9B模型
  • 【Cesium】三、实现开场动画效果
  • #渗透测试#红蓝攻防#红队打点web服务突破口总结01
  • 适用于项目经理的跨团队协作实践:Atlassian Jira与Confluence集成
  • 智能家居体验大变革 博联 AI 方案让智能不再繁琐
  • 云计算与服务是什么
  • 接口测试面试题
  • 【Cesium】六、实现鹰眼地图(三维)与主图联动效果
  • ESLint+Prettier的配置
  • 4.微服务灰度发布落地实践(消息队列增强)
  • 【从零开始入门unity游戏开发之——C#篇35】C#自定义类实现Sort自定义排序
  • 音频进阶学习九——离散时间傅里叶变换DTFT
  • 连接github和ai的桥梁:GitIngest
  • Pytorch使用手册-DCGAN 指南(专题十四)
  • Flume的安装和使用
  • [Hive]七 Hive 内核
  • Druid密码错误重试导致数据库超慢
  • Ubuntu 24.04安装和使用WPS 2019