Vuejs-Vue datetime: vue-datetime - Mobile friendly datetime picker for Vue. Supports date, datetime and time modes, i18n and disabling dates.

vue-datetime

Software License Latest Version on NPM npm Vue 2.x Build Coverage

Mobile friendly datetime picker for Vue. Supports date, datetime and time modes, i18n and more.

Demo

Go to demo.

demo

Installation

Bundler (Webpack, Rollup...)

yarn add luxon vue-datetime weekstart

Or

npm install --save luxon vue-datetime weekstart

weekstart is optional, is used to get the first day of the week.

Register

import Vue from 'vue'
import { Datetime } from 'vue-datetime'
// You need a specific loader for CSS files
import 'vue-datetime/dist/vue-datetime.css'

Vue.use(Datetime)

Register manually

Global
import { Datetime } from 'vue-datetime';

Vue.component('datetime', Datetime);
Local
import { Datetime } from 'vue-datetime';

Vue.extend({
  template: '...',
  components: {
    datetime: Datetime
  }
});

Browser

Download vue, luxon, weekstart and vue-datetime or use a CDN like unpkg.

<link rel="stylesheet" href="vue-datetime.css"></link>
<script src="vue.js"></script>
<script src="luxon.js"></script>
<script src="weekstart.js"></script>
<script src="vue-datetime.js"></script>

The component registers itself automatically as <datetime>. If you want to use a different name then register it explicitly:

Vue.component('vue-datetime', window.VueDatetime.Datetime);

weekstart is optional, is used to get the first day of the week.

Usage

Minimal

<datetime v-model="date"></datetime>

Setup

Parameters

Parameter Type Default Description
v-model (required) ISO 8601 String - Datetime.
type String date Picker type: date, datetime or time.
input-id String '' Id for the input.
input-class String, Array or Object '' Class for the input.
input-style String, Array or Object '' Style for the input.
hidden-name String null Name for hidden input with raw value. See #51.
value-zone String UTC Time zone for the value.
zone String local Time zone for the picker.
format Object or String DateTime.DATE_MED, DateTime.DATETIME_MED or DateTime.TIME_24_SIMPLE Input date format. Luxon presets or tokens.
phrases Object {ok: 'Ok', cancel: 'Cancel'} Phrases.
use12-hour Boolean false Display 12 hour (AM/PM) mode
hour-step Number 1 Hour step.
minute-step Number 1 Minute step.
min-datetime ISO 8601 String null Minimum datetime.
max-datetime ISO 8601 String null Maximum datetime.
auto Boolean false Auto continue/close on select.
week-start Number auto from locale if weekstart is available or 1 First day of the week. 1 is Monday and 7 is Sunday.
flow Array Depends of type Customize steps flow, steps available: time, date, month, year. Example: ['year', 'date', 'time']
title String '' Popup title.

Input inherits all props not defined above but style and class will be inherited by root element. See inheritAttrs option

The component is based on Luxon, check out documentation to set time zones and format.

Internationalization

Date internationalization depends on luxon. Set the default locale.

import { Settings } from 'luxon'

Settings.defaultLocale = 'es'

Events

Component emits the input event to work with v-model. More info.

close event is emitted when the popup closes.

Also, input text inherits all component events.

Slots

You can customize the component using named slots.

Available slots: before, after, button-cancel and button-confirm

Button customization example:

<datetime v-model="date" input-id="startDate">
  <label for="startDate" slot="before">Field Label</label>
  <span class="description" slot="after">The field description</span>
  <template slot="button-cancel">
    <fa :icon="['far', 'times']"></fa>
    Cancel
  </template>
  <template slot="button-confirm">
    <fa :icon="['fas', 'check-circle']"></fa>
    Confirm
  </template>
</datetime>

You can also use slot-scope to determine which view is currently active:

<template slot="button-confirm" slot-scope="scope">
  <span v-if='scope.step === "date"'>Next <i class='fas fa-arrow-right' /></span>
  <span v-else><i class='fas fa-check-circle' /> Publish</span>
</template>

Theming

Theming is supported by overwriting CSS classes.

Development

Launch lint and tests

yarn test

Launch visual tests

yarn dev

Build

Bundle the js and css to the dist folder:

yarn build

License

The MIT License

Comments

  • Select only Year or Year/Month
    Select only Year or Year/Month

    Oct 19, 2021

    I have to make a DateTime selector but according to what the user has chosen before i only need a year or year + month ( or year + month + day ^^ ).

    can use this components to do this ? ( i already use it, so i didn't want to use another components only for this page )

    Reply
  • Emit an 'open' event when the popup is opened
    Emit an 'open' event when the popup is opened

    Oct 21, 2021

    This solves issue #137 using the same approach as pull request #175. A README update and test is included.

    To pass tests I had to update the default locale test in DatetimeCalendar.spec.js. The test was rendering each weekday's name without a period ex. Lun but it was expecting a period Lun.. I suspect this was due to changes outside of this project's scope. Hopefully this change isn't a problem.

    Reply
  • Datetime component fires this.emitInput() upon creation - Solved
    Datetime component fires this.emitInput() upon creation - Solved

    Oct 23, 2021

    Hi! ?

    Firstly, thanks for your work on this project! ?

    Today I used patch-package to patch [email protected] for the project I'm working on.

    Fixed issue https://github.com/mariomka/vue-datetime/issues/46

    Here is the diff that solved my problem:

    diff --git a/node_modules/vue-datetime/src/Datetime.vue b/node_modules/vue-datetime/src/Datetime.vue
    index a8d9e99..4c28881 100644
    --- a/node_modules/vue-datetime/src/Datetime.vue
    +++ b/node_modules/vue-datetime/src/Datetime.vue
    @@ -206,7 +206,8 @@ export default {
             datetime = startOfDay(datetime)
           }
     
    -      this.$emit('input', datetime ? datetime.toISO() : '')
    +      const value = datetime ? datetime.toISO() : null
    +      this.datetime !== value ? this.$emit('input', value) : null
         },
         open (event) {
           event.target.blur()
    
    Reply
  • Fix for #267
    Fix for #267

    Nov 8, 2021

    null

                                                                                                                                                                                                           
    Reply
  • React porting
    React porting

    Nov 11, 2021

    Hello

    Is it possible to use vue-datetime as a React Component, do you have a Porting available?

    With the same styling, because React datepicker doesn't have a good styling as default.

    Reply
  • Firefox: Trapped inside calendar when using keyboard tab
    Firefox: Trapped inside calendar when using keyboard tab

    Nov 12, 2021

    Versions

    • vue: 2.6.11
    • vue-datetime: 1.0.0-beta.10
    • luxon: 1.22.0
    • Firefox: 94.0.1

    Description:

    When accessing vue-datetime in Firefox via tabbing on the keyboard, it is not possible to tab to the next element and therefore close the calendar. The user can only tab to the previous component but cannot move forward.

    Steps To Reproduce:

    Visit the official demo website and tab through while using Firefox: https://mariomka.github.io/vue-datetime/

    Reply
  • Please and typescript support
    Please and typescript support

    Jun 14, 2019

    Please and typescript support

    Reply
  • wrong min-datetime by using timezone and different date from utc
    wrong min-datetime by using timezone and different date from utc

    Dec 3, 2018

    Versions

    • vue: edge(2.5.17)
    • vue-datetime: 1.0.0-beta.8
    • luxon: 1.3.0

    Description:

    I set value-zone and zone and min-datetime props. Timezone is Asia/Tokyo(UTC+09:00). min-datetime props is wrong work, when min-datetime props is set to a value that is different between utc date and date of specified time zone.

    example

    min-datetime | expect(Asia/Tokyo) | actual(Asia/Tokyo) -- | -- | -- 2018-12-10T14:59Z | 2018-12-10T23:59+09:00 | 2018-12-10T23:59+09:00 2018-12-10T15:00Z | 2018-12-11T00:00+09:00 | 2018-12-10T00:00+09:00

    Otherwise, an excellent component!

    Steps To Reproduce:

    https://jsfiddle.net/5frzbmka/1/

    bug 
    Reply
  • Format option doesn't work
    Format option doesn't work

    Jun 11, 2019

    Latest version of Vue JS and the plugin is at the latest version.

    The format option doesn't appear to work, this is my setup:

    <datetime type="date" v-model="formData.testDate" input-class="form-control" format="DateTime.DATE_SHORT"></datetime>
    

    My output, here's an image of the input:

    Screenshot 2019-06-11 at 13 49 28

    Reply
  • CSS Styles not loading?
    CSS Styles not loading?

    Dec 14, 2018

    Versions

    • vue: 2.5.17
    • vue-datetime: ^1.0.0-beta.8
    • luxon: ^1.8.2

    Description:

    CSS Styles not loading?

    Steps To Reproduce:

    Including a reproduction would be great. You can fork this fiddle: https://jsfiddle.net/ofc30uv3/ Created Template where I'm using your awesome module:

    <template>
        <div>
    ...
                   <div class="form-group col-12 col-sm-6 col-md-4 col-lg-4 col-xl-3">
                       <label for="request.away_end_date">Letzter Tag der Abwesenheit</label>
                       <datetime type="date" v-model="request.away_end_date" id="request.away_end_date" placeholder="Datum auswählen"></datetime>
                    </div>
    ...
        </div>
    </template>
    <script>
        import {Datetime} from 'vue-datetime'
        // You need a specific loader for CSS files
        import 'vue-datetime/dist/vue-datetime.css'
        // use(Datetime);
        import { Settings } from 'luxon'
        Settings.defaultLocale = 'de';
    
    ...
    </script>
    

    Added this to the app.scss

    // Datetime CSS
    @import '~vue-datetime/dist/vue-datetime.css';
    

    I checked the folder there is an empty vue-datetime.css. I dont think thats the way it should be, right? Folder: node_modules/vue-datetime/dist/vue-datetime.css

    Reply
  • Question: Use as Time Picker only
    Question: Use as Time Picker only

    Apr 10, 2018

    Is it possible to use only as Time Picker?

    feature request 
    Reply
  • Error in page after pack files
    Error in page after pack files

    Nov 22, 2017

    Hi, I use vue-datetime and moment in my project, and I change the attribute locale of the datetime component to value zh-cn, then it's ok when I run in npm run dev. However, I pack all files by npm run build and open the page, there is error like this when click the datetime DOM:

    TypeError: Cannot read property 'weekdaysMin' of null at Object.r [as weekdays] (index.js:formatted:13497) at s.weekdays (index.js:formatted:13497) at fs.get (index.js:formatted:13497) at fs.evaluate (index.js:formatted:13497) at s.weekdays (index.js:formatted:3642) at s.render (index.js:formatted:13497) at s.e._render (index.js:formatted:13497) at s.r (index.js:formatted:3480) at fs.get (index.js:formatted:13497) at fs.run (index.js:formatted:13497)

    Would you help to check what wrong? Thank you.

    bug 
    Reply