跳转到内容

DatePicker 日期选择器

何时使用

当用户需要输入一个日期,可以点击标准输入框,弹出日期面板进行选择。

代码演示

基本

loading

范围选择器

loading

多选

loading

选择确认

loading

切换不同的选择器

loading

日期格式

loading

日期时间选择

loading

格式对齐

loading

日期限定范围

loading

禁用

loading

不可选择日期和时间

loading

允许留空

loading

选择不超过一定的范围

loading

预设范围

loading

额外的页脚

loading

三种大小

loading

定制单元格

loading

定制面板

loading

外部使用面板

loading

自定义状态

loading

形态变体

loading

弹出位置

loading

前后缀

loading

API

日期类组件包括以下五种形式。

  • DatePicker
  • DatePicker[picker="month"]
  • DatePicker[picker="week"]
  • DatePicker[picker="year"]
  • DatePicker[picker="quarter"] (4.1.0 新增)
  • RangePicker

WARNING

在搭配 Next.js 的 App Router 使用时,注意在引入 dayjs 的 locale 文件时加上 'use client'。这是由于 Ant Design 的组件都是客户端组件,在 RSC 中引入 dayjs 的 locale 文件将不会在客户端生效。

共同的 API

以下 API 为 DatePicker、 RangePicker 共享的 API。

参数说明类型默认值版本
allowClear自定义清除按钮boolean | { clearIcon?: VNode }true5.8.0: 支持对象类型
autofocus自动获取焦点booleanfalse
classs选择器 classstring-
dateRender自定义日期单元格的内容,5.4.0 起用 cellRender 代替function(currentDate: dayjs, today: dayjs) => VNode-< 5.4.0
cellRender自定义单元格的内容(current: dayjs, info: { originNode: VNode,today: DateType, range?: 'start' | 'end', type: PanelMode, locale?: Locale, subType?: 'hour' | 'minute' | 'second' | 'meridiem' }) => VNode-5.4.0
components自定义面板Record<Panel | 'input'>-5.14.0
disabled禁用booleanfalse
disabledDate不可选择的日期(currentDate: dayjs, info: { from?: dayjs, type: Picker }) => boolean-info: 5.14.0
format设置日期格式,为数组时支持多格式匹配,展示以第一个为准。配置参考 dayjs#format。示例:自定义格式formatType@rc-component/picker
order多选、范围时是否自动排序booleantrue5.14.0
preserveInvalidOnBlur失去焦点是否要清空输入框内无效内容booleanfalse5.14.0
getPopupContainer定义浮层的容器,默认为 body 上新建 divfunction(trigger)-
inputReadOnly设置输入框为只读(避免在移动设备上打开虚拟键盘)booleanfalse
locale国际化配置object默认配置
minDate最小日期,同样会限制面板的切换范围dayjs-5.14.0
maxDate最大日期,同样会限制面板的切换范围dayjs-5.14.0
mode日期面板的状态time | date | month | year | decade-
needConfirm是否需要确认按钮,为 false 时失去焦点即代表选择。当设置 multiple 时默认为 falseboolean-5.14.0
nextIcon自定义下一个图标VNode-4.17.0
open(v-model)控制弹层是否展开boolean-
panelRender自定义渲染面板(panelNode) => VNode-4.5.0
picker设置选择器类型date | week | month | quarter | yeardatequarter: 4.1.0
placeholder输入框提示文字string | [string, string]-
placement选择框弹出的位置bottomLeft bottomRight topLeft topRightbottomLeft
prefix自定义前缀VNode-5.22.0
prevIcon自定义上一个图标VNode-4.17.0
presets预设时间范围快捷选择, 自 5.8.0 起 value 支持函数返回值{ label: VNode, value: Dayjs | (() => Dayjs) }[]-
size输入框大小,large 高度为 40px,small 为 24px,默认是 32pxlarge | middle | small-
status设置校验状态'error' | 'warning'-4.19.0
style自定义输入框样式CSSProperties{}
suffixIcon自定义的选择框后缀图标VNode-
superNextIcon自定义 >> 切换图标VNode-4.17.0
superPrevIcon自定义 << 切换图标VNode-4.17.0
variant形态变体outlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.0
onOpenChange弹出日历和关闭日历的回调function(open)-
onPanelChange日历面板切换的回调function(value, mode)-

共同的方法

名称描述版本
blur()移除焦点
focus()获取焦点

DatePicker

参数说明类型默认值版本
disabledTime不可选择的时间function(date)-
format展示的日期格式,配置参考 dayjs#formatformatTypeYYYY-MM-DD
multiple是否为多选,不支持 showTimebooleanfalse5.14.0
pickerValue面板日期,可以用于受控切换面板所在日期。配合 onPanelChange 使用。dayjs-5.14.0
renderExtraFooter在面板中添加额外的页脚(mode) => VNode-
showNow显示当前日期时间的快捷选择boolean-
showTime增加时间选择功能Object | booleanTimePicker Options
showTime.defaultValue设置用户选择日期时默认的时分秒,例子dayjsdayjs()
showWeekDatePicker 下展示当前周booleanfalse5.14.0
value(v-model)日期dayjs-
onChange时间发生变化的回调function(date: dayjs, dateString: string)-
onOk点击确定按钮的回调function()-
onPanelChange日期面板变化时的回调function(value, mode)-

DatePicker[picker=year]

参数说明类型默认值版本
format展示的日期格式,配置参考 dayjs#formatformatTypeYYYY
multiple是否为多选booleanfalse5.14.0
renderExtraFooter在面板中添加额外的页脚() => VNode-
value(v-model)日期dayjs-
onChange时间发生变化的回调,发生在用户选择时间时function(date: dayjs, dateString: string)-

DatePicker[picker=quarter]

4.1.0 新增。

参数说明类型默认值版本
format展示的日期格式,配置参考 dayjs#formatformatTypeYYYY-\QQ
multiple是否为多选booleanfalse5.14.0
renderExtraFooter在面板中添加额外的页脚() => VNode-
value(v-model)日期dayjs-
onChange时间发生变化的回调,发生在用户选择时间时function(date: dayjs, dateString: string)-

DatePicker[picker=month]

参数说明类型默认值版本
format展示的日期格式,配置参考 dayjs#formatformatTypeYYYY-MM
multiple是否为多选booleanfalse5.14.0
renderExtraFooter在面板中添加额外的页脚() => VNode-
value(v-model)日期dayjs-
onChange时间发生变化的回调,发生在用户选择时间时function(date: dayjs, dateString: string)-

DatePicker[picker=week]

参数说明类型默认值版本
format展示的日期格式,配置参考 dayjs#formatformatTypeYYYY-wo
multiple是否为多选booleanfalse5.14.0
renderExtraFooter在面板中添加额外的页脚(mode) => VNode-
value(v-model)日期dayjs-
onChange时间发生变化的回调,发生在用户选择时间时function(date: dayjs, dateString: string)-
showWeekDatePicker 下展示当前周booleantrue5.14.0

RangePicker

参数说明类型默认值版本
allowEmpty允许起始项部分为空[boolean, boolean][false, false]
cellRender自定义单元格的内容。(current: dayjs, info: { originNode: VNode,today: DateType, range?: 'start' | 'end', type: PanelMode, locale?: Locale, subType?: 'hour' | 'minute' | 'second' | 'meridiem' }) => VNode-5.4.0
dateRender自定义日期单元格的内容,5.4.0 起用 cellRender 代替function(currentDate: dayjs, today: dayjs) => VNode-< 5.4.0
disabled禁用起始项[boolean, boolean]-
disabledTime不可选择的时间function(date: dayjs, partial:start|end, info: { from?: dayjs })-info.from: 5.17.0
format展示的日期格式,配置参考 dayjs#formatformatTypeYYYY-MM-DD HH:mm:ss
id设置输入框 id 属性。{ start?: string, end?: string }-5.14.0
pickerValue面板日期,可以用于受控切换面板所在日期。配合 onPanelChange 使用。dayjs[]-5.14.0
presets预设时间范围快捷选择,自 5.8.0 起 value 支持函数返回值{ label: VNode, value: (Dayjs | (() => Dayjs))\[] }\[] -
renderExtraFooter在面板中添加额外的页脚() => VNode-
separator设置分隔符VNode<SwapRightOutlined />
showTime增加时间选择功能Object|booleanTimePicker Options
showTime.defaultValue设置用户选择日期时默认的时分秒,例子dayjs[][dayjs(), dayjs()]
value(v-model)日期dayjs[]-
onCalendarChange待选日期发生变化的回调。info 参数自 4.4.0 添加function(dates: [dayjs, dayjs], dateStrings: [string, string], info: { range:start|end })-
onChange日期范围发生变化的回调function(dates: \[dayjs, dayjs], dateStrings: \[string, string])-
onFocus聚焦时回调 function(event, { range: 'start' | 'end' })-range: 5.14.0
onBlur失焦时回调 function(event, { range: 'start' | 'end' })-range: 5.14.0

formatType

typescript
import type { Dayjs } from 'dayjs';

type Generic = string;
type GenericFn = (value: Dayjs) => string;

export type FormatType =
  | Generic
  | GenericFn
  | Array<Generic | GenericFn>
  | {
      format: string;
      type?: 'mask';
    };

FAQ

如何修改周的起始日?

请使用正确的(#5605),或者修改 dayjs 的 locale 配置:https://codesandbox.io/s/dayjs-day-of-week-x9tuj2?file=/demo.tsx

js
import dayjs from 'dayjs';

import 'dayjs/locale/zh-cn';

import updateLocale from 'dayjs/plugin/updateLocale';

dayjs.extend(updateLocale);
dayjs.updateLocale('zh-cn', {
  weekStart: 0,
});

基于 MIT 许可发布