Mobile-friendly Datetime Picker for Vue 2
vue-datetime is a mobile-friendly datetime picker component designed for Vue 2 applications. Currently in beta (v1.0.0-beta.14), it provides robust functionalities for selecting dates, datetimes, or times, alongside comprehensive internationalization (i18n) support and the capability to disable specific date ranges. The library depends on Luxon for its underlying date and time manipulation and optionally utilizes `weekstart` to determine the first day of the week, though locale-based auto-detection is available. Distribution occurs via npm, enabling integration within modern JavaScript bundler workflows like Webpack or Rollup, or direct inclusion in browser environments via CDN. While it is under active development, users should be aware of its beta status, which implies that API changes may occur prior to a stable 1.0 release. Its key differentiators include its focus on mobile usability and a highly customizable picker flow and display format, making it suitable for responsive web applications.
Common errors
-
[Vue warn]: Unknown custom element: <datetime> - did you register the component correctly?
cause The `Datetime` component was not registered globally via `Vue.use()` or locally in the `components` option of a Vue component.fixRegister `Datetime` globally with `Vue.use(Datetime)` after importing it, or locally within your component's `components` option: `components: { Datetime }`. -
Module not found: Error: Can't resolve 'luxon' in '...'
cause The `luxon` peer dependency, required by `vue-datetime` for date manipulation, is missing from your project's `node_modules`.fixInstall `luxon` as a project dependency: `npm install luxon` or `yarn add luxon`.
Warnings
- breaking The package is currently in a beta state (1.0.0-beta.14). API contracts, configuration options, and internal behavior are subject to change without major version bumps until a stable 1.0 release is made. Users should expect potential breaking changes between beta versions.
- gotcha This component has peer dependencies on `luxon` (for date handling) and `vue` (the framework itself). While `weekstart` is also a peer dependency, it's optional; `vue-datetime` will auto-detect the week's start day from the locale if `weekstart` is not provided. Failure to install `luxon` will result in runtime errors.
- gotcha The component's default styling requires an explicit CSS import: `import 'vue-datetime/dist/vue-datetime.css'`. Without this, the picker will appear unstyled and potentially non-functional visually.
- gotcha The `v-model` property expects an ISO 8601 formatted string (e.g., `'2026-04-19T10:00:00.000Z'`). Providing other date formats might lead to unexpected behavior, parsing errors, or the picker not displaying the value correctly.
Install
-
npm install vue-datetime -
yarn add vue-datetime -
pnpm add vue-datetime
Imports
- Datetime
import Datetime from 'vue-datetime'
import { Datetime } from 'vue-datetime' - CSS styles
import 'vue-datetime/dist/vue-datetime.css'
- Vue.use(Datetime)
import { use } from 'vue'; use(Datetime);import Vue from 'vue'; import { Datetime } from 'vue-datetime'; Vue.use(Datetime);
Quickstart
<template>
<div id="app">
<h1>Vue Datetime Picker Demo</h1>
<datetime v-model="selectedDate" type="datetime" input-id="my-datetime-picker"></datetime>
<p>Selected Datetime (ISO 8601): <strong>{{ selectedDate }}</strong></p>
<p>Using type="date":</p>
<datetime v-model="selectedDateOnly" type="date"></datetime>
<p>Selected Date: <strong>{{ selectedDateOnly }}</strong></p>
</div>
</template>
<script>
import Vue from 'vue';
import { Datetime } from 'vue-datetime';
import 'vue-datetime/dist/vue-datetime.css';
// Luxon is a peer dependency but doesn't need to be imported directly here.
// Ensure it's installed: npm install luxon
// Register the component globally
Vue.use(Datetime);
export default {
name: 'App',
data() {
return {
selectedDate: new Date().toISOString(), // Initialize with current ISO string
selectedDateOnly: new Date().toISOString().split('T')[0] + 'T00:00:00.000Z'
};
}
};
</script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f8f8f8;
}
h1 {
color: #333;
margin-bottom: 20px;
}
p {
margin-top: 15px;
font-size: 1.1em;
}
strong {
color: #007bff;
}
/* Basic styling for the picker input */
#my-datetime-picker {
padding: 8px 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
width: 250px;
margin-bottom: 20px;
}
</style>