Skip to content

Datepicker

A light weight vue datepicker plugin.

Demo


Install

bash
npm i @vuesimple/vs-datepicker

Usage

html
<template>
  <vs-datepicker
    label="Date"
    id="deliveryDate"
    v-model="form.date"
    placeholder="DD-MM-YYYY"
    format="DD-MM-YYYY"
  ></vs-datepicker>
</template>

<script>
  import VsDatepicker from '@vuesimple/vs-datepicker';

  export default {
    components: {
      VsDatepicker,
    },
  };
</script>

CDN

html
<script src="https://unpkg.com/@vuesimple/vs-datepicker@<version>/dist/index.min.js"></script>

TIP

Replace <version> with a version number in the above url.

javascript
// Main/Entry file
app.use(VsDatepicker.plugin);
html
<template>
  <vs-datepicker
    label="Date"
    id="deliveryDate"
    v-model="form.date"
    placeholder="DD-MM-YYYY"
    format="DD-MM-YYYY"
  ></vs-datepicker>
</template>

Nuxt.js

After installation,

  • Create a file /plugins/vs-datepicker.js

    javascript
    import Vue from 'vue';
    import VsDatepicker from '@vuesimple/vs-datepicker';
    
    Vue.component('vs-datepicker', VsDatepicker);
  • Update nuxt.config.js

    javascript
    module.exports = {
      ...
      plugins: [
        { src: '~plugins/vs-datepicker', mode: 'client' }
        ...
      ]
    }
  • In the page/ component

    html
    <template>
      <vs-datepicker
        label="Date"
        id="deliveryDate"
        v-model="form.date"
        placeholder="DD-MM-YYYY"
        format="DD-MM-YYYY"
      ></vs-datepicker>
    </template>

Note

  • For older Nuxt versions, use <no-ssr>...</no-ssr> tag.
  • You can also do import VsDatepicker from '@vuesimple/vs-datepicker' & add in component:{VsDatepicker} and use it within component, without globally installing in plugin folder.

Props

NameTypeDefaultDescription
typedate,datetime,year,month,time,weekdateSelect the type of picker
formatTokenMM-DD-YYYYTo set the date format. similar to moment.js
value-typeValue-Type'date'Data type of the binding value/v-model.
rangeBooleanfalseIf true, pick the range date
labelStringDateLabel to datepicker
isErrorBoolean-Highlight input box with red if error
requiredBoolean-show * with label
disabledBoolean-Disable datepicker
disabled-dateFunction-Disable dates in datepicker
idString-Id added to datepicker component
placeholderString-Placeholder to datepicker input box
langObject-Override the default locale
editableBooleanfalseWhether the input is editable
clearableBooleanfalseIf false, don't show the clear icon
is-compactBooleanfalseApplies compact style to datepicker

NOTE Example for maxdate

html
<vs-datepicker
  label="Date"
  id="deliveryDate"
  v-model="form.date"
  placeholder="DD-MM-YYYY"
  format="DD-MM-YYYY"
  :disabled-date="disabledDate"
></vs-datepicker>

<script>
  export default {
    methods: {
      disabledDates(date) {
        return date < new Date('Your Min Date') || date > new Date('Your Max Date');
      },
    },
  };
</script>

Events

NameDescription
v-modelWhen the value changes
changeWhen the value change(same as v-model)
openWhen panel opening
closeWhen panel closing

Token

UnitTokenOutput
YearYY70 71 ... 10 11
YYYY1970 1971 ... 2010 2011
Y-1000 ...20 ... 1970 ... 9999 +10000
MonthM1 2 ... 11 12
MM01 02 ... 11 12
MMMJan Feb ... Nov Dec
MMMMJanuary February ... November December
Day of MonthD1 2 ... 30 31
DD01 02 ... 30 31
Day of Weekd0 1 ... 5 6
ddSu Mo ... Fr Sa
dddSun Mon ... Fri Sat
ddddSunday Monday ... Friday Saturday
AM/PMAAM PM
aam pm
HourH0 1 ... 22 23
HH00 01 ... 22 23
h1 2 ... 12
hh01 02 ... 12
Minutem0 1 ... 58 59
mm00 01 ... 58 59
Seconds0 1 ... 58 59
ss00 01 ... 58 59
Fractional SecondS0 1 ... 8 9
SS00 01 ... 98 99
SSS000 001 ... 998 999
Time ZoneZ-07:00 -06:00 ... +06:00 +07:00
ZZ-0700 -0600 ... +0600 +0700
Week of Yearw1 2 ... 52 53
ww01 02 ... 52 53
Unix TimestampX1360013296
Unix Millisecond Timestampx1360013296123

Value Type

ValueDescription
'date'Returns a Date object
'timestamp'Returns a timestamp number
'format'Returns a string formatted using pattern of format
token(MM/DD/YYYY)Returns a string formatted using this pattern

Full lang config

javascript
{
  // the locale of formatting and parsing function
  formatLocale: {
    // MMMM
    months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    // MMM
    monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    // dddd
    weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
    // ddd
    weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    // dd
    weekdaysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
    // first day of week
    firstDayOfWeek: 0,
    // first week contains January 1st.
    firstWeekContainsDate: 1,
  },
}

Original credits to Vue2 Datepicker