Appearance
DatePicker 日期选择框
输入或选择日期的控件。
代码演示
基础用法
最简单的用法,在浮层中可以选择或者输入日期。
vue
<template>
<a-space direction="vertical" :size="12">
<a-date-picker v-model:value="value1" />
<a-date-picker v-model:value="value2" picker="week" />
<a-date-picker v-model:value="value3" picker="month" />
<a-date-picker v-model:value="value4" picker="quarter" />
<a-date-picker v-model:value="value5" picker="year" />
</a-space>
</template>
<script setup>
import { ref } from 'vue';
const value1 = ref();
const value2 = ref();
const value3 = ref();
const value4 = ref();
const value5 = ref();
</script>范围选择器
通过设置 picker 属性,指定范围选择器类型。
vue
<template>
<a-space direction="vertical" :size="12">
<a-range-picker v-model:value="value1" />
<a-range-picker v-model:value="value2" show-time />
<a-range-picker v-model:value="value3" picker="week" />
<a-range-picker v-model:value="value4" picker="month" />
<a-range-picker v-model:value="value5" picker="year" />
</a-space>
</template>
<script setup>
import { ref } from 'vue';
const value1 = ref();
const value2 = ref();
const value3 = ref();
const value4 = ref();
const value5 = ref();
</script>日期格式
使用 format 属性,可以自定义日期显示格式。
vue
<template>
<a-space direction="vertical" :size="12">
<a-date-picker v-model:value="value1" :format="dateFormat" />
<a-date-picker v-model:value="value2" :format="dateFormatList" />
<a-date-picker v-model:value="value3" :format="monthFormat" picker="month" />
<a-range-picker v-model:value="value4" :format="dateFormat" />
<a-date-picker v-model:value="value5" :format="customFormat" />
<a-date-picker v-model:value="value6" :format="customWeekStartEndFormat" picker="week" />
</a-space>
</template>
<script setup>
import dayjs from 'dayjs';
import { ref } from 'vue';
const dateFormat = 'YYYY/MM/DD';
const weekFormat = 'MM/DD';
const monthFormat = 'YYYY/MM';
const dateFormatList = ['DD/MM/YYYY', 'DD/MM/YY'];
const customWeekStartEndFormat = value =>
`${dayjs(value).startOf('week').format(weekFormat)} ~ ${dayjs(value)
.endOf('week')
.format(weekFormat)}`;
const value1 = ref(dayjs('2015/01/01', dateFormat));
const value2 = ref(dayjs('01/01/2015', dateFormatList[0]));
const value3 = ref(dayjs('2015/01', monthFormat));
const value4 = ref([dayjs('2015/01/01', dateFormat), dayjs('2015/01/01', dateFormat)]);
const value5 = ref(dayjs('2015/01/01', dateFormat));
const value6 = ref(dayjs());
const customFormat = value => `custom format: ${value.format(dateFormat)}`;
</script>日期时间选择
增加选择时间功能,当 showTime 为一个对象时,其属性会传递给内建的 TimePicker。
vue
<template>
<a-space direction="vertical" :size="12">
<a-date-picker show-time placeholder="Select Time" @change="onChange" @ok="onOk" />
<a-range-picker
:show-time="{ format: 'HH:mm' }"
format="YYYY-MM-DD HH:mm"
:placeholder="['Start Time', 'End Time']"
@change="onRangeChange"
@ok="onRangeOk"
/>
</a-space>
</template>
<script setup>
const onChange = (value, dateString) => {
console.log('Selected Time: ', value);
console.log('Formatted Selected Time: ', dateString);
};
const onOk = value => {
console.log('onOk: ', value);
};
const onRangeChange = (value, dateString) => {
console.log('Selected Time: ', value);
console.log('Formatted Selected Time: ', dateString);
};
const onRangeOk = value => {
console.log('onOk: ', value);
};
export {};
</script>禁用
选择框的不可用状态。
vue
<template>
<a-space direction="vertical">
<a-date-picker v-model:value="value1" />
<a-date-picker v-model:value="value2" disabled picker="month" />
<a-range-picker v-model:value="value3" disabled />
<a-range-picker v-model:value="value4" :disabled="[false, true]" />
</a-space>
</template>
<script setup>
import dayjs from 'dayjs';
import { ref } from 'vue';
const dateFormat = 'YYYY-MM-DD';
const value1 = ref(dayjs('2015-06-06', dateFormat));
const value2 = ref(dayjs('2015-06', 'YYYY-MM'));
const value3 = ref([dayjs('2015-06-06', dateFormat), dayjs('2015-06-06', dateFormat)]);
const value4 = ref([dayjs('2019-09-03', dateFormat), dayjs('2019-11-22', dateFormat)]);
</script>受控面板
通过组合 mode 与 onPanelChange 控制要展示的面板。
vue
<template>
<a-space direction="vertical" :size="12">
<a-date-picker
:mode="mode1"
show-time
@openChange="handleOpenChange1"
@panelChange="handlePanelChange1"
/>
<a-range-picker
:placeholder="['Start month', 'End month']"
format="YYYY-MM"
:value="value"
:mode="mode2"
@panelChange="handlePanelChange2"
@change="handleChange"
/>
</a-space>
</template>
<script setup>
import { ref } from 'vue';
const mode1 = ref('time');
const mode2 = ref(['month', 'month']);
const value = ref();
const handleOpenChange1 = open => {
if (open) {
mode1.value = 'time';
}
};
const handleChange = val => {
value.value = val;
};
const handlePanelChange1 = (_val, mode) => {
mode1.value = mode;
};
const handlePanelChange2 = (val, mode) => {
value.value = val;
mode2.value = [mode[0] === 'date' ? 'month' : mode[0], mode[1] === 'date' ? 'month' : mode[1]];
};
</script>自定义状态
使用 status 为 DatePicker 添加状态,可选 error 或者 warning。
vue
<template>
<a-space direction="vertical" style="width: 100%">
<a-date-picker status="error" />
<a-date-picker status="warning" />
<a-range-picker status="error" />
<a-range-picker status="warning" />
</a-space>
</template>弹出位置
可以通过 placement 手动指定弹出的位置。
vue
<template>
<a-radio-group v-model:value="placement">
<a-radio-button value="topLeft">topLeft</a-radio-button>
<a-radio-button value="topRight">topRight</a-radio-button>
<a-radio-button value="bottomLeft">bottomLeft</a-radio-button>
<a-radio-button value="bottomRight">bottomRight</a-radio-button>
</a-radio-group>
<br />
<br />
<a-space direction="vertical" :size="12">
<a-date-picker v-model:value="value1" :placement="placement" />
<a-range-picker v-model:value="value2" :placement="placement" />
</a-space>
</template>
<script setup>
import { ref } from 'vue';
const placement = ref('topLeft');
const value1 = ref();
const value2 = ref();
</script>API
日期类组件包括以下五种形式。
- DatePicker
- DatePicker[picker="month"]
- DatePicker[picker="week"]
- DatePicker[picker="year"]
- DatePicker[picker="quarter"]
- RangePicker
国际化配置
默认配置为 en-US,如果你需要设置其他语言,推荐在入口处使用我们提供的国际化组件。
如有特殊需求(仅修改单一组件的语言),请使用 locale 参数,参考:默认配置。
vue
<template>
<a-date-picker v-model:value="value" :locale="locale" />
</template>
<script>
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN';
import { defineComponent } from 'vue';
export default defineComponent({
setup() {
return {
locale,
value: null,
};
},
});
</script>vue
<template>
<a-config-provider :locale="locale">
<a-date-picker v-model:value="value" />
</a-config-provider>
</template>
<script>
// 默认语言为 en-US,如果你需要设置其他语言,推荐在入口文件全局设置 locale
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN';
import { defineComponent } from 'vue';
dayjs.locale('zh-cn');
export default defineComponent({
setup() {
return {
value: dayjs('2015-01-01', 'YYYY-MM-DD')
dayjs,
locale
};
},
});
</script>共同的 API
以下 API 为 DatePicker、 RangePicker 共享的 API。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| allowClear | 是否显示清除按钮 | boolean | true |
| autofocus | 自动获取焦点 | boolean | false |
| bordered | 是否有边框 | boolean | true |
| dateRender | 自定义日期单元格的内容 | v-slot:dateRender="{current, today}" | - |
| disabled | 禁用 | boolean | false |
| disabledDate | 不可选择的日期 | (currentDate: dayjs) => boolean | - |
| format | 设置日期格式,为数组时支持多格式匹配,展示以第一个为准。配置参考 dayjs,支持自定义格式 | formatType | YYYY-MM-DD |
| popupClassName | 额外的弹出日历 className | string | - |
| getPopupContainer | 定义浮层的容器,默认为 body 上新建 div | function(trigger) | - |
| inputReadOnly | 设置输入框为只读(避免在移动设备上打开虚拟键盘) | boolean | false |
| locale | 国际化配置 | object | 默认配置 |
| mode | 日期面板的状态 | time | date | month | year | decade | - |
| nextIcon | 自定义下一个图标 | slot | - |
| open | 控制弹层是否展开 | boolean | - |
| picker | 设置选择器类型 | date | week | month | quarter | year | date |
| placeholder | 输入框提示文字 | string | [string, string] | - |
| placement | 选择框弹出的位置 | bottomLeft bottomRight topLeft topRight | bottomLeft |
| popupStyle | 额外的弹出日历样式 | CSSProperties | {} |
| prevIcon | 自定义上一个图标 | slot | - |
| presets | 预设时间范围快捷选择 | { label: slot, value: dayjs }[] | - |
| size | 输入框大小,large 高度为 40px,small 为 24px,默认是 32px | large | middle | small | - |
| status | 设置校验状态 | 'error' | 'warning' | - |
| suffixIcon | 自定义的选择框后缀图标 | v-slot:suffixIcon | - |
| superNextIcon | 自定义 << 切换图标 | slot | - |
| superPrevIcon | 自定义 >> 切换图标 | slot | - |
| valueFormat | 可选,绑定值的格式,对 value、defaultValue、defaultPickerValue 起作用。不指定则绑定值为 dayjs 对象 | string,具体格式 | - |
共有的事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| openChange | 弹出日历和关闭日历的回调 | function(status) |
| panelChange | 日期面板变化时的回调 | function(value, mode) |
共同的方法
| 名称 | 描述 |
|---|---|
| blur() | 移除焦点 |
| focus() | 获取焦点 |
DatePicker
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| defaultPickerValue | 默认面板日期 | dayjs | - |
| disabledTime | 不可选择的时间 | function(date) | - |
| format | 展示的日期格式,配置参考 dayjs | formatType | YYYY-MM-DD |
| renderExtraFooter | 在面板中添加额外的页脚 | v-slot:renderExtraFooter="mode" | - |
| showNow | 当设定了 showTime 的时候,面板是否显示“此刻”按钮 | boolean | - |
| showTime | 增加时间选择功能 | Object | boolean | TimePicker Options |
| showTime.defaultValue | 设置用户选择日期时默认的时分秒 | dayjs | dayjs() |
| showToday 是否展示“今天”按钮 | boolean | true | |
| value(v-model) | 日期 | dayjs | - |
DatePicker 事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| change | 时间发生变化的回调 | function(date: dayjs | string, dateString: string) |
| ok | 点击确定按钮的回调 | function(date: dayjs | string) |
DatePicker[picker=year]
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| format | 展示的日期格式,配置参考 dayjs | formatType | YYYY |
DatePicker[picker=quarter]
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| format | 展示的日期格式,配置参考 dayjs | formatType | YYYY-\QQ |
DatePicker[picker=month]
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| format | 展示的日期格式,配置参考 dayjs | formatType | YYYY-MM |
| monthCellRender | 自定义的月份内容渲染方法 | v-slot:monthCellRender="{current, locale}" | - |
DatePicker[picker=week]
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| format | 展示的日期格式,配置参考 dayjs | formatType | YYYY-wo |
RangePicker
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| allowEmpty | 允许起始项部分为空 | [boolean, boolean] | [false, false] |
| dateRender | 自定义日期单元格的内容。 | v-slot:dateRender="{current: dayjs, today: dayjs, info: { range: start | end }}" |
| defaultPickerValue | 默认面板日期 | dayjs[] | - |
| disabled | 禁用起始项 | [boolean, boolean] | - |
| disabledTime | 不可选择的时间 | function(date: dayjs, partial: start | end) | - |
| format | 展示的日期格式 | formatType | YYYY-MM-DD HH:mm:ss |
| presets | 预设时间范围快捷选择 | { label: slot, value: dayjs[] }[] | - |
| ranges | 预设时间范围快捷选择 | { [range: string]: dayjs[] } | { [range: string]: () => dayjs[] } | - |
| renderExtraFooter | 在面板中添加额外的页脚 | v-slot:renderExtraFooter="mode" | - |
| separator | 设置分隔符 | string | v-slot:separator | |
| showTime | 增加时间选择功能 | Object | boolean | TimePicker Options |
| showTime.defaultValue | 设置用户选择日期时默认的时分秒 | dayjs[] | [dayjs(), dayjs()] |
| value(v-model) | 日期 | dayjs[] | - |
RangePicker 事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| calendarChange | 待选日期发生变化的回调 | function(dates: [dayjs, dayjs] | [string, string], dateStrings: [string, string], info: { range:start |
| change | 日期范围发生变化的回调 | function(dates: [dayjs, dayjs] | [string, string], dateStrings: [string, string]) |
| ok | 点击确定按钮的回调 | function(dates: [dayjs, dayjs] | [string, string]) |
formatType
ts
import type { Dayjs } from 'dayjs';
type Generic = string;
type GenericFn = (value: Dayjs) => string;
export type FormatType = Generic | GenericFn | Array<Generic | GenericFn>;
