Home

Awesome

⚠️ No longer maintained. Sorry everyone, as you see I couldn't maintain this project properly. Feel free to use it/copy it to improve this datepicker.

vue-datepicker

Codacy Badge Codacy Badge

A datepicker Vue component. Compatible with Vue 2.x

Demo

To view a demo online: https://vue-datepicker.netlify.com/

Install

npm install @mathieustan/vue-datepicker --save

or

yarn add @mathieustan/vue-datepicker
import { VueDatePicker } from '@mathieustan/vue-datepicker';
import '@mathieustan/vue-datepicker/dist/vue-datepicker.min.css';

export default {
  // ...
  components: {
    VueDatePicker
  }
  // ...
}

or

import VueDatePicker from '@mathieustan/vue-datepicker';
import '@mathieustan/vue-datepicker/dist/vue-datepicker.min.css';

Vue.use(VueDatePicker);

Usage

<VueDatePicker v-model="currentDate" />

value prop if passed should be a Date object

<template>
    <VueDatePicker :value="date"></VueDatePicker>
</template>
<script>
export default {
  // ...
  components: {
    VueDatePicker
  },
  data: () => ({
      date: new Date([2019, 5, 16]),
  }),,
  // ...
}
</script>

support name attribute for normal html form submission

<VueDatePicker :value="state.date" name="uniquename"></VueDatePicker>

Using v-model

<VueDatePicker v-model="state.date" name="uniquename"></VueDatePicker>

Available props

PropTypeDefaultDescription
idString'datepicker'Sets the input id
valueDate|String|NumberDate picker model (ISO 8601 format, YY-mm-dd or YY-mm)
nameString'datepicker'Input name property & datepicker title in fullscreenMobile
clearableBooleanfalseAdd input clear functionality
validateBooleanfalseShows validations button to select date
buttonValidateString'Ok'Sets validate text button
buttonCancelString'Cancel'Sets cancel text button
formatStringDD MMMM YYYYAllows you to customize input date format
formatHeaderStringdddd DD MMMAllows you to customize header date format
placeholderString'YYYY-MM-DD'Sets the input’s placeholder text
visibleBooleanfalseAllows you to trigger datepicker visibility
disabledBooleanfalseSets datepicker disabled
colorString#4f88ffAllows you to customize color
contentClassString''Applies custom class to datepicker content
noHeaderBooleanHides datepicker header
noInputBooleanHides datepicker input. It'll show a button instead
noCalendarIconBooleanHides datepicker icon
fullscreenMobileBooleanfalseShows datepicker in a bottom sheet when in mobile view
allowedDatesFunctionFunction which validate allowed date (allowedDates: (date) => .... // date is a javascript date)
minDateString|Number|DateDates are availables after this date
maxDateString|Number|DateDates are disabled after this date
visibleYearsNumberNumber10Allows to set years range in year picker (it will add X years before & after current year)
localeObject{lang: 'en'}Allows to change language. More informations here Translations
inlineBooleanfalseShows an inline datepicker
attachString|DOM ElementfalseSpecifies which DOM element that this component should detach to.String can be any valid querySelector and Object can be any valid Node. This will attach to the root body component by default. Best Practices
typeString'date'Determines the type of the picker - date/month/quarter
rangeBooleanfalseSets range mode active (currently, only available for date)
rangeInputTextString'%d ~ %d'Sets input text format
rangeHeaderTextString'From %d To %d 'Sets header text format
rangePresetsArrayAllows to add custom range presets to range datepicker. (Max custom presets = 6) Example: [{ name: 'This year', dates: { start: ..., end: ... } }]
tabindexString|Number'0'Specify input tabindex
originStringAllows to set origin (otherwise, it will be generated from position)
rtlBooleanfalseAllow to use datepicker with rtl mode
zIndexNumberSpecify a z-index to datepicker & overlay

Events

EventOutputDescription
onOpendatepicker opened
onClosedatepicker closed
onChangedatepicker changed
onDestroydatepicker destroyed

Date Formatting

You can use this with format props.

Example :

<VueDatePicker v-model="currentDate" format="YYYY-MM-DD" />
FormatOutputDescription
YY18Two-digit year
YYYY2018Four-digit year
M1-12Month, beginning at 1
MM01-12Month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31Day of month
DD01-31Day of month, 2-digits
H0-23Hours
HH00-23Hours, 2-digits
h1-12Hours, 12-hour clock
hh01-12Hours, 12-hour clock, 2-digits
m0-59Minutes
mm00-59Minutes, 2-digits
s0-59Seconds
ss00-59Seconds, 2-digits
S0-9Hundreds of milliseconds, 1-digit
SS00-99Tens of milliseconds, 2-digits
SSS000-999Milliseconds, 3-digits
Z-5:00Offset from UTC
ZZ-0500Compact offset from UTC, 2-digits
AAM PMPost or ante meridiem, upper-case
aam pmPost or ante meridiem, lower-case
Do1st... 31stDay of Month with ordinal

Translations

By default, locale will be set from your navigator language. But it's possible to set it from locale props. Actually, there are some locale inside project ('en', 'fr, 'es', 'de');

exemple:

<template>
    <VueDatePicker :value="date" :locale="locale"></VueDatePicker>
</template>
<script>
export default {
  // ...
  components: {
    VueDatePicker
  },
  data: () => ({
      date: new Date([2019, 5, 16]),
      locale: {
        lang: 'fr' // or 'en', 'es', 'de',
        weekDays: ['L', 'M', 'M', 'J', 'V', 'S', 'D'], // you can surcharge weekDays too
      },
  }),,
  // ...
}
</script>

:rocket: Now, you can use your own language object

Below, an exemple for it language : You should have those properties to make it works :

'name', 'weekdays', 'weekdaysShort', 'weekdaysMin', 'weekStart', 'months', 'monthsShort', 'ordinal', 'formats', 'buttonCancel', 'buttonValidate', 'rangeHeaderText'
<template>
    <VueDatePicker :value="date" :locale="locale"></VueDatePicker>
</template>
<script>
const locale = {
  lang: {
    name: 'it',
    weekdays: 'domenica_lunedì_martedì_mercoledì_giovedì_venerdì_sabato'.split('_'),
    weekdaysShort: 'dom_lun_mar_mer_gio_ven_sab'.split('_'),
    weekStart: 1,
    months: 'gennaio_febbraio_marzo_aprile_maggio_giugno_luglio_agosto_settembre_ottobre_novembre_dicembre'.split('_'),
    monthsShort: 'gen_feb_mar_apr_mag_giu_lug_ago_set_ott_nov_dic'.split('_'),
    formats: {
      LT: 'HH:mm',
      LTS: 'HH:mm:ss',
      L: 'DD/MM/YYYY',
      LL: 'D MMMM YYYY',
      LLL: 'D MMMM YYYY HH:mm',
      LLLL: 'dddd D MMMM YYYY HH:mm',
    },
    ordinal: n => `${n}º`,
    buttonCancel: 'Annulla',
    buttonValidate: 'Ok',
    rangeHeaderText: 'Dalle %d Alle 13',
  },
};

export default {
  // ...
  components: {
    VueDatePicker
  },
  data: () => ({
    date: new Date([2019, 5, 16]),
    locale,
  }),
  // ...
};
</script>

If you need more exemples, you can go to this link to find your language : -> locales

Best Practices

When should you use attach prop :

Attach

is great when you have a fixed element in your page like a panel, a dialog or a fixed fullscreen container. The datepicker position will not take care of scroll page position and it'll be absolutely positioned into your fixed container. (if you scroll, datepicker popup will scroll too).