Vant DatePicker Component
Used to select date, usually used with the Popup component.
When to Invoke
Invoke this skill when:
- User needs to select a date
- User wants to select year, month, or day separately
- User needs to customize date format
- User wants to filter or format date options
- User asks about date range constraints
Features
- Flexible Columns: Select year, month, day, or any combination
- Date Range: Min and max date constraints
- Custom Formatting: Format option text
- Option Filtering: Filter available options
- Toolbar: Customizable toolbar with confirm and cancel
- Loading State: Show loading indicator for async data
API Reference
Props
| Attribute |
Description |
Type |
Default |
| v-model |
Current date |
string[] |
[] |
| columns-type |
Columns type |
string[] |
['year', 'month', 'day'] |
| min-date |
Min date |
Date |
Ten years ago on January 1 |
| max-date |
Max date |
Date |
Ten years later on December 31 |
| title |
Toolbar title |
string |
'' |
| confirm-button-text |
Text of confirm button |
string |
Confirm |
| cancel-button-text |
Text of cancel button |
string |
Cancel |
| show-toolbar |
Whether to show toolbar |
boolean |
true |
| loading |
Whether to show loading prompt |
boolean |
false |
| readonly |
Whether to be readonly |
boolean |
false |
| filter |
Option filter |
(type: string, options: PickerOption[], values: string[]) => PickerOption[] |
- |
| formatter |
Option formatter |
(type: string, option: PickerOption) => PickerOption |
- |
| option-height |
Option height, supports px vw vh rem unit, default px |
number | string |
44 |
| visible-option-num |
Count of visible columns |
number | string |
6 |
| swipe-duration |
Duration of the momentum animation, unit ms |
number | string |
1000 |
Events
| Event |
Description |
Arguments |
| confirm |
Emitted when the confirm button is clicked |
{ selectedValues, selectedOptions, selectedIndexes } |
| cancel |
Emitted when the cancel button is clicked |
{ selectedValues, selectedOptions, selectedIndexes } |
| change |
Emitted when current option is changed |
{ selectedValues, selectedOptions, selectedIndexes, columnIndex } |
Slots
| Name |
Description |
SlotProps |
| toolbar |
Custom toolbar content |
- |
| title |
Custom title |
- |
| confirm |
Custom confirm button text |
- |
| cancel |
Custom cancel button text |
- |
| option |
Custom option content |
option: PickerOption, index: number |
| columns-top |
Custom content above columns |
- |
| columns-bottom |
Custom content below columns |
- |
Methods
Use ref to get DatePicker instance and call instance methods.
| Name |
Description |
Attribute |
Return value |
| confirm |
Stop scrolling and emit confirm event |
- |
- |
| getSelectedDate |
Get current selected date |
- |
string[] |
Usage Examples
Basic Usage
<template>
<van-date-picker
v-model="currentDate"
title="Choose Date"
:min-date="minDate"
:max-date="maxDate"
/>
</template>
<script setup>
import { ref } from 'vue'
const currentDate = ref(['2021', '01', '01'])
const minDate = new Date(2020, 0, 1)
const maxDate = new Date(2025, 5, 1)
</script>
Columns Type
<template>
<van-date-picker
v-model="currentDate"
title="Choose Year-Month"
:min-date="minDate"
:max-date="maxDate"
:columns-type="columnsType"
/>
</template>
<script setup>
import { ref } from 'vue'
const currentDate = ref(['2021', '01'])
const columnsType = ['year', 'month']
const minDate = new Date(2020, 0, 1)
const maxDate = new Date(2025, 5, 1)
</script>
Options Formatter
<template>
<van-date-picker
v-model="currentDate"
title="Choose Year-Month"
:min-date="minDate"
:max-date="maxDate"
:formatter="formatter"
:columns-type="columnsType"
/>
</template>
<script setup>
import { ref } from 'vue'
const currentDate = ref(['2021', '01'])
const columnsType = ['year', 'month']
const formatter = (type, option) => {
if (type === 'year') {
option.text += ' Year'
}
if (type === 'month') {
option.text += ' Month'
}
return option
}
const minDate = new Date(2020, 0, 1)
const maxDate = new Date(2025, 5, 1)
</script>
Options Filter
<template>
<van-date-picker
v-model="currentDate"
title="Choose Year-Month"
:filter="filter"
:min-date="minDate"
:max-date="maxDate"
:columns-type="columnsType"
/>
</template>
<script setup>
import { ref } from 'vue'
const currentDate = ref(['2021', '01'])
const columnsType = ['year', 'month']
const filter = (type, options) => {
if (type === 'month') {
return options.filter((option) => Number(option.value) % 6 === 0)
}
return options
}
const minDate = new Date(2020, 0, 1)
const maxDate = new Date(2025, 5, 1)
</script>
With Popup
<template>
<van-field
v-model="result"
is-link
readonly
name="datePicker"
label="Date Picker"
placeholder="Select date"
@click="showPicker = true"
/>
<van-popup v-model:show="showPicker" destroy-on-close position="bottom">
<van-date-picker
:model-value="pickerValue"
@confirm="onConfirm"
@cancel="showPicker = false"
/>
</van-popup>
</template>
<script setup>
import { ref } from 'vue'
const result = ref('')
const showPicker = ref(false)
const pickerValue = ref([])
const selectedValues }) => {
result.value = selectedValues.join('/')
pickerValue.value = selectedValues
showPicker.value = false
}
</script>
Best Practices
Date Range: Always set appropriate min-date and max-date to constrain user selection.
Columns Type: Use columns-type to show only the columns you need (e.g., ['year', 'month'] for year-month selection).
Formatter: Use formatter to add units or customize option text for better user experience.
Filter: Use filter to restrict available options (e.g., only show specific months).
With Popup: Combine with Popup component for better mobile experience.
v-model Format: The v-model value is an array of strings in the format ['year', 'month', 'day'].
1---2name: van-date-picker3description: Date picker component for selecting dates. Invoke when user needs to implement date selection with year, month, and day columns.4---56# Vant DatePicker Component78Used to select date, usually used with the Popup component.910## When to Invoke1112Invoke this skill when:13- User needs to select a date14- User wants to select year, month, or day separately15- User needs to customize date format16- User wants to filter or format date options17- User asks about date range constraints1819## Features2021- **Flexible Columns**: Select year, month, day, or any combination22- **Date Range**: Min and max date constraints23- **Custom Formatting**: Format option text24- **Option Filtering**: Filter available options25- **Toolbar**: Customizable toolbar with confirm and cancel26- **Loading State**: Show loading indicator for async data2728## API Reference2930### Props3132| Attribute | Description | Type | Default |33|-----------|-------------|------|---------|34| v-model | Current date | `string[]` | `[]` |35| columns-type | Columns type | `string[]` | `['year', 'month', 'day']` |36| min-date | Min date | `Date` | Ten years ago on January 1 |37| max-date | Max date | `Date` | Ten years later on December 31 |38| title | Toolbar title | `string` | `''` |39| confirm-button-text | Text of confirm button | `string` | `Confirm` |40| cancel-button-text | Text of cancel button | `string` | `Cancel` |41| show-toolbar | Whether to show toolbar | `boolean` | `true` |42| loading | Whether to show loading prompt | `boolean` | `false` |43| readonly | Whether to be readonly | `boolean` | `false` |44| filter | Option filter | `(type: string, options: PickerOption[], values: string[]) => PickerOption[]` | - |45| formatter | Option formatter | `(type: string, option: PickerOption) => PickerOption` | - |46| option-height | Option height, supports `px` `vw` `vh` `rem` unit, default `px` | `number \| string` | `44` |47| visible-option-num | Count of visible columns | `number \| string` | `6` |48| swipe-duration | Duration of the momentum animation, unit `ms` | `number \| string` | `1000` |4950### Events5152| Event | Description | Arguments |53|-------|-------------|-----------|54| confirm | Emitted when the confirm button is clicked | `{ selectedValues, selectedOptions, selectedIndexes }` |55| cancel | Emitted when the cancel button is clicked | `{ selectedValues, selectedOptions, selectedIndexes }` |56| change | Emitted when current option is changed | `{ selectedValues, selectedOptions, selectedIndexes, columnIndex }` |5758### Slots5960| Name | Description | SlotProps |61|------|-------------|-----------|62| toolbar | Custom toolbar content | - |63| title | Custom title | - |64| confirm | Custom confirm button text | - |65| cancel | Custom cancel button text | - |66| option | Custom option content | `option: PickerOption, index: number` |67| columns-top | Custom content above columns | - |68| columns-bottom | Custom content below columns | - |6970### Methods7172Use ref to get DatePicker instance and call instance methods.7374| Name | Description | Attribute | Return value |75|------|-------------|-----------|--------------|76| confirm | Stop scrolling and emit confirm event | - | - |77| getSelectedDate | Get current selected date | - | `string[]` |7879## Usage Examples8081### Basic Usage8283```vue84<template>85 <van-date-picker86 v-model="currentDate"87 title="Choose Date"88 :min-date="minDate"89 :max-date="maxDate"90 />91</template>9293<script setup>94import { ref } from 'vue'9596const currentDate = ref(['2021', '01', '01'])97const minDate = new Date(2020, 0, 1)98const maxDate = new Date(2025, 5, 1)99</script>100```101102### Columns Type103104```vue105<template>106 <van-date-picker107 v-model="currentDate"108 title="Choose Year-Month"109 :min-date="minDate"110 :max-date="maxDate"111 :columns-type="columnsType"112 />113</template>114115<script setup>116import { ref } from 'vue'117118const currentDate = ref(['2021', '01'])119const columnsType = ['year', 'month']120const minDate = new Date(2020, 0, 1)121const maxDate = new Date(2025, 5, 1)122</script>123```124125### Options Formatter126127```vue128<template>129 <van-date-picker130 v-model="currentDate"131 title="Choose Year-Month"132 :min-date="minDate"133 :max-date="maxDate"134 :formatter="formatter"135 :columns-type="columnsType"136 />137</template>138139<script setup>140import { ref } from 'vue'141142const currentDate = ref(['2021', '01'])143const columnsType = ['year', 'month']144145const formatter = (type, option) => {146 if (type === 'year') {147 option.text += ' Year'148 }149 if (type === 'month') {150 option.text += ' Month'151 }152 return option153}154155const minDate = new Date(2020, 0, 1)156const maxDate = new Date(2025, 5, 1)157</script>158```159160### Options Filter161162```vue163<template>164 <van-date-picker165 v-model="currentDate"166 title="Choose Year-Month"167 :filter="filter"168 :min-date="minDate"169 :max-date="maxDate"170 :columns-type="columnsType"171 />172</template>173174<script setup>175import { ref } from 'vue'176177const currentDate = ref(['2021', '01'])178const columnsType = ['year', 'month']179180const filter = (type, options) => {181 if (type === 'month') {182 return options.filter((option) => Number(option.value) % 6 === 0)183 }184 return options185}186187const minDate = new Date(2020, 0, 1)188const maxDate = new Date(2025, 5, 1)189</script>190```191192### With Popup193194```vue195<template>196 <van-field197 v-model="result"198 is-link199 readonly200 name="datePicker"201 label="Date Picker"202 placeholder="Select date"203 @click="showPicker = true"204 />205 <van-popup v-model:show="showPicker" destroy-on-close position="bottom">206 <van-date-picker207 :model-value="pickerValue"208 @confirm="onConfirm"209 @cancel="showPicker = false"210 />211 </van-popup>212</template>213214<script setup>215import { ref } from 'vue'216217const result = ref('')218const showPicker = ref(false)219const pickerValue = ref([])220221const onConfirm = ({ selectedValues }) => {222 result.value = selectedValues.join('/')223 pickerValue.value = selectedValues224 showPicker.value = false225}226</script>227```228229## Best Practices2302311. **Date Range**: Always set appropriate `min-date` and `max-date` to constrain user selection.2322332. **Columns Type**: Use `columns-type` to show only the columns you need (e.g., `['year', 'month']` for year-month selection).2342353. **Formatter**: Use `formatter` to add units or customize option text for better user experience.2362374. **Filter**: Use `filter` to restrict available options (e.g., only show specific months).2382395. **With Popup**: Combine with Popup component for better mobile experience.2402416. **v-model Format**: The v-model value is an array of strings in the format `['year', 'month', 'day']`.