Element UI【组件拓展】el-datetime-picker-before 禁止选择未来时间的日期时间选择器(精确到时分秒)

简介: Element UI【组件拓展】el-datetime-picker-before 禁止选择未来时间的日期时间选择器(精确到时分秒)

               

el-datetime-picker-before.vue

<template>
    <div>
        <el-date-picker
                value-format="yyyy-MM-dd"
                @change="dateChange"
                :size="size"
                :picker-options="dateOptions"
                v-model="date"
                type="date"
                placeholder="选择日期">
        </el-date-picker>
        <el-time-picker
                v-if="showTimePicker"
                value-format="HH:mm:ss"
                @change="timeChange"
                :size="size"
                :picker-options="timeOptions"
                v-model="time"
                placeholder="选择时间">
        </el-time-picker>
    </div>
</template>
<script>
    export default {
        model: {
            prop: 'value',
            event: 'updateDateTime'
        },
        methods: {
            dateChange(newDate) {
                // 从其他日期切换到今天
                if ((newDate === new Date().format('yyyy-MM-dd')) && (this.time > new Date().format("hh:mm:ss"))) {
                    this.time = '00:00:00'
                }
                this.updateTimeOptions()
                this.$emit('updateDateTime', this.date + ' ' + this.time)
            },
            timeChange() {
                this.$emit('updateDateTime', this.date + ' ' + this.time)
            },
            updateTimeOptions() {
                if (this.date === new Date().format('yyyy-MM-dd')) {
                    this.$set(this.timeOptions, 'selectableRange', "00:00:00-" + new Date().format("hh:mm:ss"))
                } else {
                    this.$set(this.timeOptions, 'selectableRange', '')
                }
                this.reloadTimePicker()
            },
            reloadTimePicker() {
                this.showTimePicker = false
                this.$nextTick(
                    () => {
                        this.showTimePicker = true
                    }
                )
            },
        },
        props: {
            value: String,
            size: {
                type: String,
                default: 'mini'
            }
        },
        mounted() {
            let dateTime = this.value.split(' ')
            this.date = dateTime[0]
            this.time = dateTime[1]
            this.updateTimeOptions()
        },
        data() {
            return {
                showTimePicker: true,
                date: '',
                time: '',
                dateOptions: {
                    //禁止选择未来的日期,不包含今天。
                    disabledDate(time) {
                        return time.getTime() > Date.now()
                    }
                },
                timeOptions: {
                    //禁止选择未来的时间,不包含当前时刻。
                    selectableRange:
                        "00:00:00-" + new Date().format("hh:mm:ss")
                },
            }
        }
    }
    // 为Date原型添加格式化方法
    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            S: this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                (this.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1
                        ? o[k]
                        : ("00" + o[k]).substr(("" + o[k]).length)
                );
            }
        }
        return fmt;
    };
</script>
<style scoped>
</style>

使用

<template>
    <div style="padding: 20px">
        <el-datetime-picker-before v-model="pubTime" />
    </div>
</template>
<script>
    import elDatetimePickerBefore from './el-datetime-picker-before'
    export default {
        components:{elDatetimePickerBefore},
        data() {
            return {
                pubTime: "2021-05-24 02:50:44",
            }
        },
    }
</script>
<style scoped>
</style>
目录
相关文章
|
2天前
Element UI【级联选择器】el-cascader 获取选中内容的 label 数据,鼠标悬浮显示超长内容
Element UI【级联选择器】el-cascader 获取选中内容的 label 数据,鼠标悬浮显示超长内容
12 3
|
1天前
|
数据安全/隐私保护
Element UI 密码输入框--可切换显示隐藏,自定义图标
Element UI 密码输入框--可切换显示隐藏,自定义图标
6 0
|
1天前
Element UI 表格【列宽自适应】
Element UI 表格【列宽自适应】
4 0
|
1天前
|
前端开发
ElementPlus卡片如何能够一行呈四,黑马UI前端布局视频资料,element样式具体的细节无法修改,F12找到那个位置,可能在其他组件写了错误,找到那个位置,围绕着位置解决问题最快了,卡片下边
ElementPlus卡片如何能够一行呈四,黑马UI前端布局视频资料,element样式具体的细节无法修改,F12找到那个位置,可能在其他组件写了错误,找到那个位置,围绕着位置解决问题最快了,卡片下边
|
1天前
Element UI 【表格合计】el-table 实战范例 -- 添加单位,自定义计算逻辑
Element UI 【表格合计】el-table 实战范例 -- 添加单位,自定义计算逻辑
6 0
|
2月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
|
2月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
|
2月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
|
2月前
|
JSON 前端开发 测试技术
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版