Vue2 Time Picker
A dropdown time picker (hour|minute|second) for Vue 2.x, with flexible time format support.
Looking for the Vue 1.x version? Please check the vue-timepicker (Vue 1.x supported)
Demo
You can see the Vue2 Timepicker in action in the Demo Page
Migration
Migrating from the Vue 1.x version? Please check MIGRATION.md for basic guidelines.
Dependencies
Vue.js v2.0+
Installation
Through NPM (Recommended)
npm install vue2-timepicker --save
Bower
bower install vue2-timepicker --save
Get Started
Step 1: Import VueTimepicker
A: Include the single file component (Recommended)
import VueTimepicker from 'vue2-timepicker'
var VueTimepicker = require('vue2-timepicker')
or, B: Include distribution files with <script>
and <style>
<script src="yourpath/vue/dist/vue.min.js"></script>
<script src="yourpath/vue2-timepicker/dist/vue2-timepicker.min.js"></script>
<link href="yourpath/vue2-timepicker/dist/vue2-timepicker.min.css" rel="stylesheet"></link>
NOTE: When using the dist
files, a Vue.use()
call is needed
Vue.use(window.VueTimepicker)
Vue.use(VueTimepicker)
Step 2: Include VueTimepicker in your component
var yourComponent = new Vue({
components: { VueTimepicker },
...
})
Step 3: Then, you can introduce the vue-timepicker
tag anywhere you like in your component's template
<vue-timepicker></vue-timepicker>
Usage
Basic Usage
<vue-timepicker></vue-timepicker>
Customized Time Format
<vue-timepicker format="HH:mm:ss"></vue-timepicker>
<vue-timepicker format="hh:mm A"></vue-timepicker>
<vue-timepicker format="hh:mm:ss a"></vue-timepicker>
VueTimepicker will recognizes the following tokens in the format string
Section | Token | Output |
---|
AM/PM | A | AM PM |
| a | am pm |
Hour | H | 0 1 ... 22 23 |
| HH | 00 01 ... 22 23 |
| h | 1 2 ... 11 12 |
| hh | 01 02 ... 11 12 |
| k | 1 2 ... 23 24 |
| kk | 01 02 ... 23 24 |
Minute | m | 0 1 ... 58 59 |
| mm | 00 01 ... 58 59 |
Second | s | 0 1 ... 58 59 |
| ss | 00 01 ... 58 59 |
If not set, format
string will be default to "HH:mm"
Customized Picker interval
<vue-timepicker :minute-interval="5"></vue-timepicker>
<vue-timepicker :second-interval="10"></vue-timepicker>
<vue-timepicker :minute-interval="yourMinuteInterval"></vue-timepicker>
Note: Please do remember to add the :
or v-bind:
sign before the interval properties
Hide Clear Button
<vue-timepicker hide-clear-button></vue-timepicker>
Bind Value with v-model
var yourComponent = new Vue({
components: { VueTimepicker },
data: function () {
return {
yourTimeValue: {
HH: "10",
mm: "05",
ss: "00"
},
...
}
},
...
})
<vue-timepicker v-model="yourTimeValue" format="HH:mm:ss"></vue-timepicker>
Get Time Picker's Current Value
Method 1: Read value from v-model
<vue-timepicker v-model="yourTimeValue" format="HH:mm:ss"></vue-timepicker>
console.log(this.yourTimeValue)
Method 2: Add @change
event handler
<vue-timepicker :time-value.sync="yourTimeValue" @change="changeHandler"></vue-timepicker>
<vue-timepicker :time-value.sync="yourTimeValue" @change="otherChangeHandler($event, 'foo', 'bar')"></vue-timepicker>
changeHandler (eventData) {
console.log(eventData)
}
otherChangeHandler (eventData, yourArg1, yourArg2) {
console.log(eventData)
console.log(yourArg1)
console.log(yourArg2)
}
Unlike v-model
, which only returns the defined time tokens you provided in the binding variable, the change
event will return all supported formats.
In the example above, when picker is set to "14:30:15" in HH:mm:ss format, change
event will return the following data:
{
HH: "14",
H: "14",
hh: "14",
a: "am",
A: "AM",
h: "14",
kk: "14",
k: "14",
m: "30",
mm: "30",
s: "15",
ss: "15"
}
Whereas the v-model
will only return the data with defined tokens
{
HH: "14",
mm: "30",
ss: "15"
}
Props API
Prop | Type | Required | Default Value |
---|
v-model | Object | no | undefined |
format | String | no | "HH:mm" |
minute-interval | Number | no | undefined |
second-interval | Number | no | undefined |
hide-clear-button | Boolean | no | false |
disabled | Boolean | no | undefined |
disabledValues | Object | no | { hours: [], minute: [], second: [], apm: [] } |
Contribution
Please feel free to fork and help developing.
npm install
npm run dev
For detailed explanation on how things work, checkout the webpack guide and docs for vue-loader.
Change Log
Detail changes for each release are documented in CHANGELOG.md
License
MIT