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;
    }
}
相关文章
Vue3日期选择器(DatePicker)
该组件基于 **@vuepic/vue-datepicker@9.0.1** 进行二次封装,简化了日常使用。除范围和年选择器外,其他日期选择均返回格式化的字符串。提供了多种自定义设置,如日期选择器宽度、模式、格式等,并支持时间选择和“今天”按钮展示。详细配置及更多功能请参考[官方文档](https://vue3datepicker.com/installation/)。组件已集成所有原生属性,并支持主题颜色自定义。 示例代码展示了如何创建和使用日期选择器组件,包括基本使用、禁用日期、日期时间选择器、范围选择器等多种场景。更多功能和样式可通过官方文档了解。
2517 2
Vue3日期选择器(DatePicker)
Vue2使用v-model封装ElementUI_DatePicker 日期选择器组件
本文介绍了如何在Vue2中使用v-model封装ElementUI的DatePicker日期选择器组件,并实现自定义属性和方法的结合使用,包括禁用日期、格式化日期等功能。
625 2
Vue2使用v-model封装ElementUI_DatePicker 日期选择器组件
|
存储 缓存 移动开发
uinapp的setStorageSync和setStorage的区别
uinapp的setStorageSync和setStorage的区别
|
资源调度 开发者
Vue2选择器(Select)
这是一个基于Vue3的选择器组件(VueAmazingSelector),已在npm上发布,方便快捷地通过`yarn add vue-amazing-selector`进行安装。此组件允许全局或局部注册,并提供了丰富的自定义属性,如选项数据、选择器文本字段名、值字段名、默认文字、禁用状态、清除功能等。同时,它还支持调整选择框的尺寸和下拉项数量,并附有详细的使用示例和代码片段,便于开发者快速集成和定制。
264 1
Vue2选择器(Select)
|
数据格式
使用小技巧实现el-table组件的合并行功能,ElementUI和ElementPlus都适用
本文介绍了在ElementUI和ElementPlus中使用`el-table`组件实现合并行功能的技巧,包括多列合并和单列合并的方法,并提供了相应的示例代码和运行效果。
9811 1
使用小技巧实现el-table组件的合并行功能,ElementUI和ElementPlus都适用
|
JavaScript
Vue2使用v-model封装ElementUI日期组件(实现开始时间和结束时间的校验,禁选)
本文介绍了如何在Vue2中使用v-model封装ElementUI日期组件,并实现开始时间和结束时间的校验,包括禁选当前时间之后的时间。文章提供了详细的组件代码和页面使用示例,并解释了如何通过props传递参数以及如何监听和处理日期选择的变化。
478 2
|
资源调度 JavaScript 前端开发
Vue2项目使用v-viewer插件实现图片预览、切换、缩放、旋转...
这篇文章介绍了在Vue 2项目中如何通过`v-viewer`插件实现图片的预览、切换、缩放、旋转等功能,并提供了插件的安装、配置和使用方法。
4031 0
Vue2项目使用v-viewer插件实现图片预览、切换、缩放、旋转...
Vue2时间轴(Timeline)
本文介绍了一个基于 Vue3 的时间轴组件 `Timeline`,允许用户自定义时间轴宽度和描述文本。通过在 `Timeline.vue` 中使用模板、脚本及样式代码,文章展示了如何创建并使用该组件,并提供了示例代码以供参考。
1136 2
Vue2时间轴(Timeline)
|
存储 JavaScript 前端开发
decimal.js库的安装和使用方法
【10月更文挑战第24天】decimal.js 是一个非常实用的高精度计算库,通过合理的安装和使用,可以在 JavaScript 中实现精确的数值计算和处理。你可以根据具体的需求和项目情况,灵活运用该库来解决数字精度丢失的问题。
|
JavaScript
基于Vue2或Vue3实现任意上下左右拖拽悬浮的元素,且配置为自定义的全局指令
这篇文章介绍了如何在Vue 2或Vue 3项目中实现一个自定义的全局指令`v-dragSwitch`,用于创建可以任意方向拖拽并悬浮的元素,同时包含边界处理的逻辑。
3782 2
基于Vue2或Vue3实现任意上下左右拖拽悬浮的元素,且配置为自定义的全局指令