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;
    }
}
相关文章
|
4月前
vue3.0 vant popup渲染不出来的解决办法
vue3.0 vant popup渲染不出来的解决办法
91 0
|
资源调度
Vue3富文本编辑器wangEditor 5使用总结
Vue3富文本编辑器wangEditor 5使用总结
780 0
|
4月前
|
JavaScript
【vue】 vue-seamless-scroll 无缝滚动依赖
【vue】 vue-seamless-scroll 无缝滚动依赖
423 1
|
21天前
Vue3日期选择器(DatePicker)
该组件基于 **@vuepic/vue-datepicker@9.0.1** 进行二次封装,简化了日常使用。除范围和年选择器外,其他日期选择均返回格式化的字符串。提供了多种自定义设置,如日期选择器宽度、模式、格式等,并支持时间选择和“今天”按钮展示。详细配置及更多功能请参考[官方文档](https://vue3datepicker.com/installation/)。组件已集成所有原生属性,并支持主题颜色自定义。 示例代码展示了如何创建和使用日期选择器组件,包括基本使用、禁用日期、日期时间选择器、范围选择器等多种场景。更多功能和样式可通过官方文档了解。
179 2
Vue3日期选择器(DatePicker)
|
2月前
|
JavaScript
vue 中安装并使用echart
vue 中安装并使用echart
30 2
|
1月前
在Vue3项目中使用 vue3-seamless-scroll 无缝滚动插件
本文介绍了如何在Vue3项目中使用`vue3-seamless-scroll`插件实现无缝滚动效果,并提供了详细的示例代码和运行效果。
720 0
|
2月前
|
JavaScript
【vue】 vue2 中使用 Tinymce 富文本编辑器
【vue】 vue2 中使用 Tinymce 富文本编辑器
244 6
|
2月前
|
JavaScript
vue 调试技巧 window.vue=this
vue 调试技巧 window.vue=this
31 0
|
4月前
|
JavaScript 容器
使用Vue写一个日期选择器
使用Vue写一个日期选择器
33 1
|
4月前
|
资源调度 JavaScript 前端开发
vue--使用wangEditor富文本
vue--使用wangEditor富文本
125 0