May 07, 2021 Vant
Time selectors, which support dimensions such as date, year, time, and so on, are typically used with pop-up layer components
import Vue from 'vue';
import { DatetimePicker } from 'vant';
Vue.use(DatetimePicker);
<van-datetime-picker
v-model="currentDate"
type="datetime"
:min-date="minDate"
:max-date="maxDate"
/>
export default {
data() {
return {
minDate: new Date(2020, 0, 1),
maxDate: new Date(2025, 10, 1),
currentDate: new Date()
};
}
};
<van-datetime-picker
v-model="currentDate"
type="date"
:min-date="minDate"
:max-date="maxDate"
/>
export default {
data() {
return {
minDate: new Date(2020, 0, 1),
maxDate: new Date(2025, 10, 1),
currentDate: new Date()
};
}
};
The option text can be formatted by passing in the formatter function
<van-datetime-picker
v-model="currentDate"
type="year-month"
:min-date="minDate"
:max-date="maxDate"
:formatter="formatter"
/>
export default {
data() {
return {
minDate: new Date(2020, 0, 1),
maxDate: new Date(2025, 10, 1),
currentDate: new Date()
};
},
methods: {
formatter(type, val) {
if (type === 'year') {
return `${val}年`;
} else if (type === 'month') {
return `${val}月`
}
return val;
}
}
}
<van-datetime-picker
v-model="currentTime"
type="time"
:min-hour="10"
:max-hour="20"
/>
export default {
data() {
return {
currentTime: '12:00'
};
}
};
By passing in the filter function, you can filter the array of options for custom intervals
<van-datetime-picker
v-model="currentTime"
type="time"
:filter="filter"
/>
export default {
data() {
return {
currentTime: '12:00'
};
},
methods: {
filter(type, options) {
if (type === 'minute') {
return options.filter(option => option % 5 === 0);
}
return options;
}
}
};
Parameters | Description | Type | The default |
---|---|---|---|
type |
Type, the optional value is
date
time
year-month
|
string |
datetime
|
title | The title of the top bar | string |
''
|
confirm-button-text | Confirm the button text | string |
确认
|
cancel-button-text | Cancel the button text | string |
取消
|
show-toolbar | Whether to display the top bar | boolean |
true
|
loading | Whether to display the load status | boolean |
false
|
filter | Option filter function | (type, vals) => vals | - |
formatter | The option formats the function | (type, val) => val | - |
item-height | The height of the option | number | string |
44
|
visible-item-count | The number of visible options | number | string |
5
|
swipe-duration
v2.2.13
|
The length of inertial scrolling when sliding quickly, in
ms
|
number | string |
1000
|
The following props are supported when the time selector type is date or datetime
Parameters | Description | Type | The default |
---|---|---|---|
min-date | Optional minimum time, accurate to minutes | Date | Ten years ago |
max-date | Optional maximum time, accurate to minutes | Date | Ten years later |
The following props are supported when the time selector type is time
Parameters | Description | Type | The default |
---|---|---|---|
min-hour | The minimum number of hours available | number | string |
0
|
max-hour | The maximum number of hours available | number | string |
23
|
min-minute | Optional minimum minute | number | string |
0
|
max-minute | The maximum number of minutes available | number | string |
59
|
The name of the event | Description | Callback parameters |
---|---|---|
change | The event that is triggered when the value changes | Picker: Picker instance |
confirm | The event that was triggered when the finish button was clicked | Value: The time currently selected |
cancel | The event that was triggered when the cancel button was clicked | - |
Ref allows you to get the DatetimePicker instance and call the instance method, as detailed in the Component Instance Method
The method name | Description | Parameters | Returns a value |
---|---|---|---|
getPicker
v2.4.0
|
Gets the Picker instance, which is used to call picker's instance method | - | - |
Be careful not to use a write like min-date""new Date()" directly in the template, which causes a new Date object to be passed in each time the component is rendered, and the new data to be passed in triggers the next rendering, which plunges into a dead loop.
The correct approach is to define min-date as a data in the data function.
If you're having trouble rendering components on iOS, make sure you're not creating a Date object with a writing like new Date ('2020-01-01'), iOS doesn't support a date format separated by a midline, which is new Date ('2020/01/01').
A detailed explanation of this problem: stackoverflow .
If you only need to select a year or month, it is recommended that you use the Picker component directly.