Vue2日期选择器插件(vue-datepicker-local)

简介: 这是一个基于 Vue 的日期选择器组件库,支持年份、月份、日期和时间的选择,并且均可进行范围选择。用户可以自定义日期格式与组件样式。该示例展示了如何配置组件以限制可选日期范围,并提供了相应的代码实现。

参考文档: https://github.com/weifeiyue/vue-datepicker-local

支持年份选择器,月份选择器,日期选择器,时间选择器,且均支持范围选择

效果如下演示年月日选择(修改组件相关默认样式,并且只能选择当前时间往前50年之内的日期)

<DatePicker
    v-model="date"
    :local="local"
    placeholder="请选择注册时间"
    format="YYYY-MM-DD"
    :disabled-date="disabledDate"
    clearable />

import DatePicker from 'vue-datepicker-local'
components: {
    DatePicker
},
data () {
    return {
        date: new Date(), // 默认选择当前日期
        local: { // 定制化日期选择的格式内容
            dow: 1, // Monday is the first day of the week
            yearSuffix: '年', // format of head
            monthsHead: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), // months of head
            months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), // months of panel
            weeks: '一_二_三_四_五_六_日'.split('_') // weeks
        },
    }
},
watch: {
    date (to, from) {
        console.log('to:', to)
        if (to === '') {
            this.checkFields('date')
        } else {
            this.checkFocus('date')
        }
    }
},
methods: {
    disabledDate (date) {
        if (date.getTime() > new Date().getTime()) { // 当前日期以后的时间禁用
            return true
        }
        // 当前时间50年前的日期禁用
        if (date.getFullYear() < new Date().getFullYear() - 50) {
            return true
        }
        if (date.getFullYear() === new Date().getFullYear() - 50 && date.getMonth() < new Date().getMonth()) {
            return true
        }
        if (date.getFullYear() === new Date().getFullYear() - 50 && date.getMonth() === new Date().getMonth() && date.getDate() < new Date().getDate()) {
            return true
        }
    }
}
// 重写组件相关样式
.datepicker {
    width: 290px;
    height: 40px;
    line-height: 40px;
    /deep/ input {
        color: #444;
        height: 40px;
        line-height: 40px;
    }
    /deep/ .focus {
        border: 1px solid @mainColor;
    }
    /deep/ .datepicker-popup {
        width: 278px;
        .calendar-head {
            width: 278px;
            .calendar-year-select, .calendar-month-select  {
                font-size: 14px;
                &:hover {
                    color: @mainColor;
                }
            }
            .calendar-prev-year-btn, .calendar-prev-month-btn, .calendar-next-year-btn, .calendar-next-month-btn {
                &:hover {
                    color: @mainColor;
                }
            }
        }
        .calendar-body {
            width: 278px;
            height: 200px;
            .calendar-date {
                line-height: 100%;
            }
            .calendar-date-selected {
                background: @mainColor;
            }
        }
        .datepicker__buttons {
            .datepicker__button-cancel {
                background: E3E3E3;
            }
            .datepicker__button-select {
                background: @mainColor;
            }
        }
    }
    .calendar-date-selected {
        background: @mainColor;
    }
}
相关文章
element-使用el-date-picker 选择日期后返回周几(整理)
element-使用el-date-picker 选择日期后返回周几(整理)
|
前端开发 JavaScript 安全
【前端相关】elementui使用el-upload组件实现自定义上传
【前端相关】elementui使用el-upload组件实现自定义上传
3814 0
|
JavaScript
【vue】 vue-seamless-scroll 无缝滚动依赖
【vue】 vue-seamless-scroll 无缝滚动依赖
2294 1
Vue3日期选择器(DatePicker)
该组件基于 **@vuepic/vue-datepicker@9.0.1** 进行二次封装,简化了日常使用。除范围和年选择器外,其他日期选择均返回格式化的字符串。提供了多种自定义设置,如日期选择器宽度、模式、格式等,并支持时间选择和“今天”按钮展示。详细配置及更多功能请参考[官方文档](https://vue3datepicker.com/installation/)。组件已集成所有原生属性,并支持主题颜色自定义。 示例代码展示了如何创建和使用日期选择器组件,包括基本使用、禁用日期、日期时间选择器、范围选择器等多种场景。更多功能和样式可通过官方文档了解。
3838 2
Vue3日期选择器(DatePicker)
|
资源调度 开发者
Vue2选择器(Select)
这是一个基于Vue3的选择器组件(VueAmazingSelector),已在npm上发布,方便快捷地通过`yarn add vue-amazing-selector`进行安装。此组件允许全局或局部注册,并提供了丰富的自定义属性,如选项数据、选择器文本字段名、值字段名、默认文字、禁用状态、清除功能等。同时,它还支持调整选择框的尺寸和下拉项数量,并附有详细的使用示例和代码片段,便于开发者快速集成和定制。
394 1
Vue2选择器(Select)
Vue2使用v-model封装ElementUI_DatePicker 日期选择器组件
本文介绍了如何在Vue2中使用v-model封装ElementUI的DatePicker日期选择器组件,并实现自定义属性和方法的结合使用,包括禁用日期、格式化日期等功能。
810 2
Vue2使用v-model封装ElementUI_DatePicker 日期选择器组件
|
JavaScript UED
【vue】iview组件 DatePicker 日期选择器如何显示默认当前日期
【vue】iview组件 DatePicker 日期选择器如何显示默认当前日期
821 1
|
存储 前端开发 Java
Element el-upload 文件上传/图片上传/拖拽上传/附带参数/附带请求头部详解
文目录 1. 前言 2. 基本用法 2.1 前端部分 2.2 后端部分 2.3 获取后端返回信息 3. 外观功能介绍 3.1 拖拽上传 3.2 显示图片 3.3 设置文件列表样式 3.4 显示提示信息 4. 事件功能介绍 4.1 限制上传文件数量 4.2 限制上传文件类型和大小 4.3 移除文件处理 4.4 手动上传 5. 附带参数 6. 附带请求头部 7. 小结
8376 0
|
自然语言处理 JavaScript 前端开发
最好用的 12 款 Vue Timepicker 时间日期选择器测评推荐 - 卡拉云
Vue 时间日期选择器(date-timepicker)组件在使用 Vue 框架开发中使用非常频繁。vue timepicker 除了基础选择日期时间外,还有非常多样的功能配合不同场景使用,比如 12/24小时,禁止选择某些日期,高亮某些日期,夜间模式,多语言,酒店订单的特别场景等。本文记录了我自己使用多年最好用的 12 款 vue timepicker 组件,每一款都经过我实际测试,推荐给大家。
1851 0
mo-quarter-picker:基于 Vue2 和 ElementUI 的季度范围选择器
mo-quarter-picker:基于 Vue2 和 ElementUI 的季度范围选择器
1325 0
mo-quarter-picker:基于 Vue2 和 ElementUI 的季度范围选择器