Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue2-datepicker

mengxiong10329.4kMIT3.11.1TypeScript support: definitely-typed

A Datepicker Component For Vue2

vue, calendar, datepicker, datetimepicker

readme

vue2-datepicker

中文版

A Datepicker Component For Vue2

build:passed Badge npm MIT

For Vue 3.0, you can use vue-datepicker-next from the same author

Demo

https://mengxiong10.github.io/vue2-datepicker/index.html

image

Install

$ npm install vue2-datepicker --save

Usage

<script>
  import DatePicker from 'vue2-datepicker';
  import 'vue2-datepicker/index.css';

  export default {
    components: { DatePicker },
    data() {
      return {
        time1: null,
        time2: null,
        time3: null,
      };
    },
  };
</script>

<template>
  <div>
    <date-picker v-model="time1" valueType="format"></date-picker>
    <date-picker v-model="time2" type="datetime"></date-picker>
    <date-picker v-model="time3" range></date-picker>
  </div>
</template>

Theme

If your project uses SCSS, you can change the default style variables.

To create a scss file. e.g. datepicker.scss:

$namespace: 'xmx'; // change the 'mx' to 'xmx'. then <date-picker prefix-class="xmx" />

$default-color: #555;
$primary-color: #1284e7;

@import '~vue2-datepicker/scss/index.scss';

Internationalization

The default language of v3.x is English. If you need other locales, you can import a locale file. Once you import a locale, it becomes the active locale.

import DatePicker from 'vue2-datepicker';
import 'vue2-datepicker/index.css';

import 'vue2-datepicker/locale/zh-cn';

You can also override some of the default locale by lang. Full config

<script>
  export default {
    data() {
      return {
        lang: {
          formatLocale: {
            firstDayOfWeek: 1,
          },
          monthBeforeYear: false,
        },
      };
    },
  };
</script>

<template>
  <date-picker :lang="lang"></date-picker>
</template>

Props

Prop Description Type Default
type select the type of picker date |datetime|year|month|time|week 'date'
range if true, pick the range date boolean false
format to set the date format. similar to moment.js token 'YYYY-MM-DD'
formatter use your own formatter, such as moment.js object -
value-type data type of the binding value value-type 'date'
default-value default date of the calendar Date new Date()
lang override the default locale object
placeholder input placeholder text string ''
editable whether the input is editable boolean true
clearable if false, don't show the clear icon boolean true
confirm if true, need click the button to change value boolean false
confirm-text the text of confirm button string 'OK'
multiple if true, multi-select date boolean false
disabled disable the component boolean false
disabled-date specify the date that cannot be selected (date: Date, currentValue: Date[]) => boolean -
disabled-time specify the time that cannot be selected (date: Date) => boolean -
append-to-body append the popup to body boolean true
inline without input boolean false
input-class input classname string 'mx-input'
input-attr input attrs(eg: { name: 'date', id: 'foo'}) object
open open state of picker boolean -
default-panel default panel of the picker year|month -
popup-style popup style object
popup-class popup classes | —
shortcuts set shortcuts to select Array<{text, onClick}> -
title-format format of the tooltip in calendar cell token 'YYYY-MM-DD'
partial-update whether update date when select year or month boolean false
range-separator text of range separator string ' ~ '
show-week-number determine whether show week number boolean false
hour-step interval between hours in time picker 1 - 60 1
minute-step interval between minutes in time picker 1 - 60 1
second-step interval between seconds in time picker 1 - 60 1
hour-options custom hour column Array<number> -
minute-options custom minute column Array<number> -
second-options custom second column Array<number> -
show-hour whether show hour column boolean base on format
show-minute whether show minute column boolean base on format
show-second whether show second column boolean base on format
use12h whether show ampm column boolean base on format
show-time-header whether show header of time picker boolean false
time-title-format format of the time header token 'YYYY-MM-DD'
time-picker-options set fixed time list to select time-picker-options null
prefix-class set prefix class string 'mx'
scroll-duration set the duration of scroll when hour is selected number 100

Token

Uint Token output
Year YY 70 71 ... 10 11
| YYYY 1970 1971 ... 2010 2011
| Y -1000 ...20 ... 1970 ... 9999 +10000
Month M 1 2 ... 11 12
| MM 01 02 ... 11 12
| MMM Jan Feb ... Nov Dec
| MMMM January February ... November December
Day of Month D 1 2 ... 30 31
| DD 01 02 ... 30 31
Day of Week d 0 1 ... 5 6
| dd Su Mo ... Fr Sa
| ddd Sun Mon ... Fri Sat
| dddd Sunday Monday ... Friday Saturday
AM/PM A AM PM
| a am pm
Hour H 0 1 ... 22 23
| HH 00 01 ... 22 23
| h 1 2 ... 12
| hh 01 02 ... 12
Minute m 0 1 ... 58 59
| mm 00 01 ... 58 59
Second s 0 1 ... 58 59
| ss 00 01 ... 58 59
Fractional Second S 0 1 ... 8 9
| SS 00 01 ... 98 99
| SSS 000 001 ... 998 999
Time Zone Z -07:00 -06:00 ... +06:00 +07:00
| ZZ -0700 -0600 ... +0600 +0700
Week of Year w 1 2 ... 52 53
| ww 01 02 ... 52 53
Unix Timestamp X 1360013296
Unix Millisecond Timestamp x 1360013296123

formatter

The formatter accepts an object to customize formatting.

<date-picker :formatter="momentFormat" />
data() {
  return {
    // Use moment.js instead of the default
    momentFormat: {
      //[optional] Date to String
      stringify: (date) => {
        return date ? moment(date).format('LL') : ''
      },
      //[optional]  String to Date
      parse: (value) => {
        return value ? moment(value, 'LL').toDate() : null
      },
      //[optional] getWeekNumber
      getWeek: (date) => {
        return // a number
      }
    }
  }
}

value-type

set the format of binding value

Value Description
'date' return a Date object
'timestamp' return a timestamp number
'format' returns a string formatted using pattern of format
token(MM/DD/YYYY) returns a string formatted using this pattern

shortcuts

The shortcuts for the range picker

[
  { text: 'today', onClick: () => new Date() },
  {
    text: 'Yesterday',
    onClick: () => {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24);
      return date;
    },
  },
];
Attribute Description
text title of the shortcut
onClick callback function , need to return a Date

time-picker-options

Set fixed time list to select;

{start: '00:00', step:'00:30' , end: '23:30', format: 'HH:mm' }
Attribute Description
start start time
step step time
end end time
format the default is same as prop format

Events

Name Description Callback Arguments
input When the value change(v-model event) date
change When the value change(same as input) date, type('date'|'hour'|'minute'|'second'|'ampm
open When panel opening event
close When panel closing
confirm When click 'confirm' button date
clear When click 'clear' button
input-error When user type a invalid Date the input text
focus When input focus
blur When input blur
pick when select date #429 date
calendar-change when change the calendar date, oldDate, type('year'|'month'|'last-year'|'next-year'|'last-month'|'next-month'|'last-decade'|'next-decade')
panel-change when the calendar panel changes type('year'|'month'|'date'), oldType

Slots

Name Description
icon-calendar custom the calender icon
icon-clear custom the clear icon
input replace input
header popup header
footer popup footer
sidebar popup sidebar

ChangeLog

CHANGELOG

One-time Donations

If you find this project useful, you can buy me a coffee

Paypal Me

donate

License

MIT

Copyright (c) 2017-present xiemengxiong

changelog

3.11.1 (2022-12-01)

Bug Fixes

  • should click twice to open popup in mobile (#725) (375edbb)

3.11.0 (2022-06-12)

Features

3.10.4 (2021-12-02)

Bug Fixes

3.10.3 (2021-11-17)

Bug Fixes

3.10.2 (2021-09-21)

Bug Fixes

  • sync selectedValue and inputValue on opening (#640) (8907810)

3.10.1 (2021-08-30)

Features

  • improve the boundary of disabledtime (#597) (071c398)

3.10.0 (2021-08-30)

3.9.2 (2021-08-08)

Bug Fixes

  • don't change default open state when datepicker disabled (#630) (a1251ff)
  • round the timezone offset (57216c1)

3.9.1 (2021-06-03)

Bug Fixes

  • move vertical-align to td, th to avoid UA style dependency (#608) (#610) (705c100)
  • update:show-time-panel event invalid (#614) (7ec739a)

Features

3.9.0 (2021-02-06)

Features

3.8.2 (2020-12-07)

Bug Fixes

3.8.1 (2020-11-26)

Bug Fixes

3.8.0 (2020-11-18)

Bug Fixes

Features

3.7.0 (2020-10-20)

Features

  • add prop formatter to replace the object usage of format (5d3d547)

3.6.3 (2020-10-17)

3.6.2 (2020-08-10)

Bug Fixes

  • provide scoped slot to input slot (#492) (b3126a0)
  • value entered manually in disabled range should be invalid (#508) (1bca35b)

3.6.1 (2020-07-13)

Bug Fixes

  • popup sholud be not auto closed in time range (a4e5019)

3.6.0 (2020-06-07)

Bug Fixes

Features

3.5.0 (2020-04-22)

Features

3.4.1 (2020-03-06)

Bug Fixes

  • split error when rangeSeparator is same as token (435a6e8)

3.4.0 (2020-03-06)

Bug Fixes

  • fix open event fired multiple times (be26d2a)
  • mobile support for date range (#419) (3c2e00e)
  • popup doesn't reposition when scrolling in Firefox (#427) (2d50e75)

Features

  • add calendar-change event (#432) (17d2262)
  • add pick event when date clicked (#429) (2fb9e67)
  • ignore whitespace around separator on manual range input (#416) (376a7ef)

3.3.1 (2020-02-18)

Bug Fixes

3.3.0 (2019-12-19)

Bug Fixes

Features

3.2.2 (2019-12-05)

Bug Fixes

  • prevent button triggering external form submit (#389) (7fcc1df)
  • prop lang should be reactive (#391) (3c76fc3)

3.2.1 (2019-12-03)

Bug Fixes

  • defaultValue should be used in datetime mode (#384) (309a1bf)

3.2.0 (2019-12-02)

Bug Fixes

Features

3.1.1 (2019-11-25)

Bug Fixes

  • panel doesn't close after picking time (#378) (8657d49)
  • when custom formatting causes the time-panel error (#377) (bf949af)

3.1.0 (2019-11-21)

Bug Fixes

  • ie style compatible (e69b9b7)
  • move sidebar element to right position (32c74a4)

Features

3.0.2 (2019-11-14)

Bug Fixes

  • compatible with vue old version inject (c03632f)
  • should return [null, null] when clear a range (635631f)
  • when year < 100 && year >= 0, the date should be right (8c546cc)

Features

  • add prop partial-update. It'll update the date when select year or month in type='date' or type='datetime' (07f4271)

3.0.1 (2019-11-11)

Bug Fixes

3.0.0 (2019-11-10)

Bug Fixes

  • localization (lang) is not used for date formatting

Features

  • Add week picker
    <date-picer type="week" />
  • Add inline calendar without input

    <date-picker inline />
  • Add prop open to control the state of popup

  • Add select am/pm, when use12hours
  • Add event open and close
  • Add hourStep/minuteStep/secondStep/showHour/showMinute/showSecond/use12h to time picker for more configuration

Breaking changes

  • Need to import style separately
  • Change the default language to English. And need to import file to change the language.
  • Range selection refactoring, you can select a range on one calendar now, and need to click twice to select a range each time.
  • Change slot calendar-icon to icon-calendar.
  • appendToBody default value changes from false to true.
  • Remove not-before and not-after, use disabledDate instead.

    <template>
      <date-picker :disabled-date="notBeforeToday"></date-picker>
      <date-picker :disabled-date="notAfterToday"></date-picker>
    </template>
    <script>
      export default {
        methods: {
          notBeforeToday(date) {
            const today = new Date();
            today.setHours(0, 0, 0, 0);
            return date.getTime() < today.getTime();
          },
          notAfterToday(date) {
            const today = new Date();
            today.setHours(0, 0, 0, 0);
            return date.getTime() > today.getTime();
          },
        },
      };
    </script>
  • Remove width, use style="{ width: "" }" instead.

  • modify shortcuts api.

    [
      { text: 'today', onClick: () => new Date() // return a Date }
    ];
    
  • Remove prop firstDayOfWeek to locale.

2.13.3 (2019-10-29)

Bug Fixes

2.13.2 (2019-10-23)

Bug Fixes

2.13.1 (2019-10-22)

Bug Fixes

2.13.0 (2019-10-14)

Bug Fixes

  • removed keyboard event stopPropagation (864ab83)
  • the clickoutside don't work sometimes (#326) (d9619f8)

Features

  • add prop icon-day to set calendar icon day (62c3d60)

2.12.0 (2019-06-25)

Features

2.11.2 (2019-05-15)

Bug Fixes

2.11.1 (2019-04-30)

Bug Fixes

  • close popup after select shortcuts (5823f85)

2.11.0 (2019-04-09)

Bug Fixes

  • clickoutside bad when use append-to-body (#291) (9bb6046)

Features

2.10.3 (2019-03-14)

Bug Fixes

2.10.2 (2019-03-14)

Bug Fixes

  • pick minute will affect the hour (#168) (2afed88)
  • remove the append background (e7775d6)

Performance Improvements

  • remove IE 10's clear button X (6a990d8)

2.10.1 (2019-03-11)

Performance Improvements

  • change the entity name to entity number (dbbba6b)

2.10.0 (2019-02-12)

Features

  • add prop default-value for calendar default date (#94) (4ff6945)
  • show the popup on focus and close it on blur (3bcedf5)

2.9.2 (2019-02-10)

Bug Fixes

  • the display error of the year panel header (#245) (7bc2785)

2.9.1 (2019-02-01)

Bug Fixes

  • stringify function returns a error value,when date is null (#244) (92243ab)

2.9.0 (2019-01-29)

Features

  • support custom format function (c801516)

2.8.1 (2019-01-24)

Bug Fixes

  • clickoutside event listener change capturing to bubbling (054758e)
  • use refs instead of children array (f43e3a3)

2.8.0 (2019-01-13)

Features

  • add valueType to format binding value (dd6f2ea)

2.7.0 (2019-01-08)

Bug Fixes

  • when clear input(not use clear button) the date value not changed (39d2c40)

Features

  • add class to .mx-calendar indicating the different panel (#219) (1d0a67b)
  • add prop inputAttr (2381089)

2.6.4 (2018-12-19)

Bug Fixes

  • when clear input(not use clear button) the date value not changed (39d2c40)

2.6.3 (2018-12-08)

Bug Fixes

  • fix unable set value later when range is ture (#209) (97289d1)

2.6.2 (2018-10-30)

Bug Fixes

  • calendar-change trigger in right time (b1a5a41)

Features

  • add calendar-change event (ef9314e)

2.6.1 (2018-10-17)

Bug Fixes

  • prevent popup internal click event from affecting the outside (de177d8)

Features

  • when use script type, install automatic (a310f59)

2.6.0 (2018-10-11)

Bug Fixes

Features

2.5.0 (2018-10-05)

Features

2.4.3 (2018-09-28)

Bug Fixes

Features

  • emit clear event for the clear button (e0776b6)

2.4.0 (2018-08-08)

Features

  • add prop time to show only time picker (1046731)

2.3.2 (2018-08-07)

Features

2.2.0 (2018-08-06)

Bug Fixes

  • set input autocomplete off (264458c)

Features

  • add event 'change-calendar-year' 'change-calendar-month' (bc80708)

2.1.0 (2018-07-24)

Bug Fixes

  • fecha.format need a date (1e54efc)
  • shortcut date changed to function (5cf6c72)

Features

  • add the slot footer slot-scope to confirm event (43d9fd7)
  • export the fecha to use (ac958d4)

2.0.0 (2018-06-16)

Features

  • Add clearable used to show clear icon
  • Add slot calendar-icon to custom calendar icon
  • Add slot header and footer to custom popup area
  • disabledDays supports custom functions

Breaking changes

  • Refactored code. This may fail if you hacked the code
  • format default value changes from yyyy-MM-dd to YYYY-MM-DD, now the parsing tokens are similar to the moment.js, not supports lowercase yyyy
  • remove custom-formatter
  • editable default value changes from false to true, now supports range
  • select the year or month panel will not change the value(Because it causes many problems when set the not-before or not-after)