DatePicker日期选择框

输入或选择日期的控件。

何时使用#

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

代码演示

最简单的用法,在浮层中可以选择或者输入日期。

expand codeexpand code
import type { DatePickerProps } from 'antd';
import { DatePicker, Space } from 'antd';
import React from 'react';

const onChange: DatePickerProps['onChange'] = (date, dateString) => {
  console.log(date, dateString);
};

const App: React.FC = () => (
  <Space direction="vertical">
    <DatePicker onChange={onChange} />
    <DatePicker onChange={onChange} picker="week" />
    <DatePicker onChange={onChange} picker="month" />
    <DatePicker onChange={onChange} picker="quarter" />
    <DatePicker onChange={onChange} picker="year" />
  </Space>
);

export default App;
Time

提供选择器,自由切换不同类型的日期选择器,常用于日期筛选场合。

expand codeexpand code
import type { DatePickerProps, TimePickerProps } from 'antd';
import { DatePicker, Select, Space, TimePicker } from 'antd';
import React, { useState } from 'react';

const { Option } = Select;

type PickerType = 'time' | 'date';

const PickerWithType = ({
  type,
  onChange,
}: {
  type: PickerType;
  onChange: TimePickerProps['onChange'] | DatePickerProps['onChange'];
}) => {
  if (type === 'time') return <TimePicker onChange={onChange} />;
  if (type === 'date') return <DatePicker onChange={onChange} />;
  return <DatePicker picker={type} onChange={onChange} />;
};

const App: React.FC = () => {
  const [type, setType] = useState<PickerType>('time');

  return (
    <Space>
      <Select value={type} onChange={setType}>
        <Option value="time">Time</Option>
        <Option value="date">Date</Option>
        <Option value="week">Week</Option>
        <Option value="month">Month</Option>
        <Option value="quarter">Quarter</Option>
        <Option value="year">Year</Option>
      </Select>
      <PickerWithType type={type} onChange={value => console.log(value)} />
    </Space>
  );
};

export default App;

增加选择时间功能,当 showTime 为一个对象时,其属性会传递给内建的 TimePicker

expand codeexpand code
import { DatePicker, Space } from 'antd';
import type { DatePickerProps, RangePickerProps } from 'antd/es/date-picker';
import React from 'react';

const { RangePicker } = DatePicker;

const onChange = (
  value: DatePickerProps['value'] | RangePickerProps['value'],
  dateString: [string, string] | string,
) => {
  console.log('Selected Time: ', value);
  console.log('Formatted Selected Time: ', dateString);
};

const onOk = (value: DatePickerProps['value'] | RangePickerProps['value']) => {
  console.log('onOk: ', value);
};

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <DatePicker showTime onChange={onChange} onOk={onOk} />
    <RangePicker
      showTime={{ format: 'HH:mm' }}
      format="YYYY-MM-DD HH:mm"
      onChange={onChange}
      onOk={onOk}
    />
  </Space>
);

export default App;

可用 disabledDatedisabledTime 分别禁止选择部分日期和时间,其中 disabledTime 需要和 showTime 一起使用。

expand codeexpand code
import { DatePicker, Space } from 'antd';
import type { RangePickerProps } from 'antd/es/date-picker';
import moment from 'moment';
import React from 'react';

const { RangePicker } = DatePicker;

const range = (start: number, end: number) => {
  const result = [];
  for (let i = start; i < end; i++) {
    result.push(i);
  }
  return result;
};

// eslint-disable-next-line arrow-body-style
const disabledDate: RangePickerProps['disabledDate'] = current => {
  // Can not select days before today and today
  return current && current < moment().endOf('day');
};

const disabledDateTime = () => ({
  disabledHours: () => range(0, 24).splice(4, 20),
  disabledMinutes: () => range(30, 60),
  disabledSeconds: () => [55, 56],
});

const disabledRangeTime: RangePickerProps['disabledTime'] = (_, type) => {
  if (type === 'start') {
    return {
      disabledHours: () => range(0, 60).splice(4, 20),
      disabledMinutes: () => range(30, 60),
      disabledSeconds: () => [55, 56],
    };
  }
  return {
    disabledHours: () => range(0, 60).splice(20, 4),
    disabledMinutes: () => range(0, 31),
    disabledSeconds: () => [55, 56],
  };
};

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <DatePicker
      format="YYYY-MM-DD HH:mm:ss"
      disabledDate={disabledDate}
      disabledTime={disabledDateTime}
      showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
    />
    <DatePicker picker="month" disabledDate={disabledDate} />
    <RangePicker disabledDate={disabledDate} />
    <RangePicker
      disabledDate={disabledDate}
      disabledTime={disabledRangeTime}
      showTime={{
        hideDisabledOptions: true,
        defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('11:59:59', 'HH:mm:ss')],
      }}
      format="YYYY-MM-DD HH:mm:ss"
    />
  </Space>
);

export default App;

可以预设常用的日期范围以提高用户体验。

expand codeexpand code
import { DatePicker, Space } from 'antd';
import type { RangePickerProps } from 'antd/es/date-picker';
import moment from 'moment';
import React from 'react';

const { RangePicker } = DatePicker;

const onChange: RangePickerProps['onChange'] = (dates, dateStrings) => {
  if (dates) {
    console.log('From: ', dates[0], ', to: ', dates[1]);
    console.log('From: ', dateStrings[0], ', to: ', dateStrings[1]);
  } else {
    console.log('Clear');
  }
};

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <RangePicker
      ranges={{
        Today: [moment(), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
      }}
      onChange={onChange}
    />
    <RangePicker
      ranges={{
        Today: [moment(), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
      }}
      showTime
      format="YYYY/MM/DD HH:mm:ss"
      onChange={onChange}
    />
  </Space>
);

export default App;

三种大小的输入框,若不设置,则为 middle

expand codeexpand code
import type { RadioChangeEvent } from 'antd';
import { DatePicker, Radio, Space } from 'antd';
import type { SizeType } from 'antd/es/config-provider/SizeContext';
import React, { useState } from 'react';

const { RangePicker } = DatePicker;

const App: React.FC = () => {
  const [size, setSize] = useState<SizeType>('middle');

  const handleSizeChange = (e: RadioChangeEvent) => {
    setSize(e.target.value);
  };

  return (
    <Space direction="vertical" size={12}>
      <Radio.Group value={size} onChange={handleSizeChange}>
        <Radio.Button value="large">Large</Radio.Button>
        <Radio.Button value="middle">middle</Radio.Button>
        <Radio.Button value="small">Small</Radio.Button>
      </Radio.Group>
      <DatePicker size={size} />
      <DatePicker size={size} picker="month" />
      <RangePicker size={size} />
      <DatePicker size={size} picker="week" />
    </Space>
  );
};

export default App;

使用 status 为 DatePicker 添加状态,可选 error 或者 warning

expand codeexpand code
import { DatePicker, Space } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <Space direction="vertical" style={{ width: '100%' }}>
    <DatePicker status="error" style={{ width: '100%' }} />
    <DatePicker status="warning" style={{ width: '100%' }} />
    <DatePicker.RangePicker status="error" style={{ width: '100%' }} />
    <DatePicker.RangePicker status="warning" style={{ width: '100%' }} />
  </Space>
);

export default App;
4.19.0




可以通过 placement 手动指定弹出的位置。

expand codeexpand code
import type { DatePickerProps, RadioChangeEvent } from 'antd';
import { DatePicker, Radio } from 'antd';
import React, { useState } from 'react';

const { RangePicker } = DatePicker;

const App: React.FC = () => {
  const [placement, SetPlacement] = useState<DatePickerProps['placement']>('topLeft');

  const placementChange = (e: RadioChangeEvent) => {
    SetPlacement(e.target.value);
  };

  return (
    <>
      <Radio.Group value={placement} onChange={placementChange}>
        <Radio.Button value="topLeft">topLeft</Radio.Button>
        <Radio.Button value="topRight">topRight</Radio.Button>
        <Radio.Button value="bottomLeft">bottomLeft</Radio.Button>
        <Radio.Button value="bottomRight">bottomRight</Radio.Button>
      </Radio.Group>
      <br />
      <br />
      <DatePicker placement={placement} />
      <br />
      <br />
      <RangePicker placement={placement} />
    </>
  );
};

export default App;

通过设置 picker 属性,指定范围选择器类型。

expand codeexpand code
import { DatePicker, Space } from 'antd';
import React from 'react';

const { RangePicker } = DatePicker;

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <RangePicker />
    <RangePicker showTime />
    <RangePicker picker="week" />
    <RangePicker picker="month" />
    <RangePicker picker="quarter" />
    <RangePicker picker="year" />
  </Space>
);

export default App;

使用 format 属性,可以自定义日期显示格式。

expand codeexpand code
import type { DatePickerProps } from 'antd';
import { DatePicker, Space } from 'antd';
import moment from 'moment';
import React from 'react';

const { RangePicker } = DatePicker;

const dateFormat = 'YYYY/MM/DD';
const weekFormat = 'MM/DD';
const monthFormat = 'YYYY/MM';

const dateFormatList = ['DD/MM/YYYY', 'DD/MM/YY'];

const customFormat: DatePickerProps['format'] = value =>
  `custom format: ${value.format(dateFormat)}`;

const customWeekStartEndFormat: DatePickerProps['format'] = value =>
  `${moment(value).startOf('week').format(weekFormat)} ~ ${moment(value)
    .endOf('week')
    .format(weekFormat)}`;

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <DatePicker defaultValue={moment('2015/01/01', dateFormat)} format={dateFormat} />
    <DatePicker defaultValue={moment('01/01/2015', dateFormatList[0])} format={dateFormatList} />
    <DatePicker defaultValue={moment('2015/01', monthFormat)} format={monthFormat} picker="month" />
    <DatePicker defaultValue={moment()} format={customWeekStartEndFormat} picker="week" />
    <RangePicker
      defaultValue={[moment('2015/01/01', dateFormat), moment('2015/01/01', dateFormat)]}
      format={dateFormat}
    />
    <DatePicker defaultValue={moment('2015/01/01', dateFormat)} format={customFormat} />
  </Space>
);

export default App;

选择框的不可用状态。你也可以通过数组单独禁用 RangePicker 的其中一项。

expand codeexpand code
import { DatePicker, Space } from 'antd';
import moment from 'moment';
import React from 'react';

const { RangePicker } = DatePicker;

const dateFormat = 'YYYY-MM-DD';

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <DatePicker defaultValue={moment('2015-06-06', dateFormat)} disabled />
    <DatePicker picker="month" defaultValue={moment('2015-06', 'YYYY-MM')} disabled />
    <RangePicker
      defaultValue={[moment('2015-06-06', dateFormat), moment('2015-06-06', dateFormat)]}
      disabled
    />
    <RangePicker
      defaultValue={[moment('2019-09-03', dateFormat), moment('2019-11-22', dateFormat)]}
      disabled={[false, true]}
    />
  </Space>
);

export default App;

这里举例如何用 onCalendarChangedisabledDate 来限制动态的日期区间选择。

expand codeexpand code
import { DatePicker } from 'antd';
import type { Moment } from 'moment';
import React, { useState } from 'react';

const { RangePicker } = DatePicker;

type RangeValue = [Moment | null, Moment | null] | null;

const App: React.FC = () => {
  const [dates, setDates] = useState<RangeValue>(null);
  const [value, setValue] = useState<RangeValue>(null);

  const disabledDate = (current: Moment) => {
    if (!dates) {
      return false;
    }
    const tooLate = dates[0] && current.diff(dates[0], 'days') > 7;
    const tooEarly = dates[1] && dates[1].diff(current, 'days') > 7;
    return !!tooEarly || !!tooLate;
  };

  const onOpenChange = (open: boolean) => {
    if (open) {
      setDates([null, null]);
    } else {
      setDates(null);
    }
  };

  return (
    <RangePicker
      value={dates || value}
      disabledDate={disabledDate}
      onCalendarChange={val => setDates(val)}
      onChange={val => setValue(val)}
      onOpenChange={onOpenChange}
      onBlur={() => console.log('blur has been triggered')}
    />
  );
};

export default App;

使用 dateRender 可以自定义日期单元格的内容和样式。

expand codeexpand code
import { DatePicker, Space } from 'antd';
import React from 'react';

const { RangePicker } = DatePicker;

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <DatePicker
      dateRender={current => {
        const style: React.CSSProperties = {};
        if (current.date() === 1) {
          style.border = '1px solid #1890ff';
          style.borderRadius = '50%';
        }
        return (
          <div className="ant-picker-cell-inner" style={style}>
            {current.date()}
          </div>
        );
      }}
    />
    <RangePicker
      dateRender={current => {
        const style: React.CSSProperties = {};
        if (current.date() === 1) {
          style.border = '1px solid #1890ff';
          style.borderRadius = '50%';
        }
        return (
          <div className="ant-picker-cell-inner" style={style}>
            {current.date()}
          </div>
        );
      }}
    />
  </Space>
);

export default App;

无边框样式。

expand codeexpand code
import { DatePicker, Space } from 'antd';
import React from 'react';

const { RangePicker } = DatePicker;

const App: React.FC = () => (
  <Space direction="vertical" size={12}>
    <DatePicker bordered={false} />
    <DatePicker picker="week" bordered={false} />
    <DatePicker picker="month" bordered={false} />
    <DatePicker picker="year" bordered={false} />
    <RangePicker bordered={false} />
    <RangePicker picker="week" bordered={false} />
    <RangePicker picker="month" bordered={false} />
    <RangePicker picker="year" bordered={false} />
  </Space>
);

export default App;

API#

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

  • DatePicker

  • DatePicker[picker="month"]

  • DatePicker[picker="week"]

  • DatePicker[picker="year"]

  • DatePicker[picker="quarter"] (4.1.0 新增)

  • RangePicker

国际化配置#

默认配置为 en-US,如果你需要设置其他语言,推荐在入口处使用我们提供的国际化组件,详见:ConfigProvider 国际化

如有特殊需求(仅修改单一组件的语言),请使用 locale 参数,参考:默认配置

import 'moment/locale/zh-cn';
import locale from 'antd/es/date-picker/locale/zh_CN';

<DatePicker locale={locale} />;
// 默认语言为 en-US,如果你需要设置其他语言,推荐在入口文件全局设置 locale
import moment from 'moment';
import 'moment/locale/zh-cn';
import locale from 'antd/es/locale/zh_CN';

<ConfigProvider locale={locale}>
  <DatePicker defaultValue={moment('2015-01-01', 'YYYY-MM-DD')} />
</ConfigProvider>;

共同的 API#

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

参数说明类型默认值版本
allowClear是否显示清除按钮booleantrue
autoFocus自动获取焦点booleanfalse
bordered是否有边框booleantrue
className选择器 classNamestring-
dateRender自定义日期单元格的内容function(currentDate: moment, today: moment) => React.ReactNode-
disabled禁用booleanfalse
disabledDate不可选择的日期(currentDate: moment) => boolean-
popupClassName额外的弹出日历 classNamestring-4.23.0
getPopupContainer定义浮层的容器,默认为 body 上新建 divfunction(trigger)-
inputReadOnly设置输入框为只读(避免在移动设备上打开虚拟键盘)booleanfalse
locale国际化配置object默认配置
mode日期面板的状态(设置后无法选择年份/月份?time | date | month | year | decade-
nextIcon自定义下一个图标ReactNode-4.17.0
open控制弹层是否展开boolean-
panelRender自定义渲染面板(panelNode) => ReactNode-4.5.0
picker设置选择器类型date | week | month | quarter | yeardatequarter: 4.1.0
placeholder输入框提示文字string | [string, string]-
placement选择框弹出的位置bottomLeft bottomRight topLeft topRightbottomLeft
popupStyle额外的弹出日历样式CSSProperties{}
prevIcon自定义上一个图标ReactNode-4.17.0
size输入框大小,large 高度为 40px,small 为 24px,默认是 32pxlarge | middle | small-
status设置校验状态'error' | 'warning'-4.19.0
style自定义输入框样式CSSProperties{}
suffixIcon自定义的选择框后缀图标ReactNode-
superNextIcon自定义 << 切换图标ReactNode-4.17.0
superPrevIcon自定义 >> 切换图标ReactNode-4.17.0
onOpenChange弹出日历和关闭日历的回调function(open)-
onPanelChange日历面板切换的回调function(value, mode)-

共同的方法#

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

DatePicker#

参数说明类型默认值版本
defaultPickerValue默认面板日期moment-
defaultValue默认日期,如果开始时间或结束时间为 null 或者 undefined,日期范围将是一个开区间moment-
disabledTime不可选择的时间function(date)-
format设置日期格式,为数组时支持多格式匹配,展示以第一个为准。配置参考 moment.js,支持自定义格式string | (value: moment) => string | (string | (value: moment) => string)[]YYYY-MM-DD
renderExtraFooter在面板中添加额外的页脚(mode) => React.ReactNode-
showNow当设定了 showTime 的时候,面板是否显示“此刻”按钮boolean-4.4.0
showTime增加时间选择功能Object | booleanTimePicker Options
showTime.defaultValue设置用户选择日期时默认的时分秒,例子momentmoment()
showToday是否展示“今天”按钮booleantrue
value日期moment-
onChange时间发生变化的回调function(date: moment, dateString: string)-
onOk点击确定按钮的回调function()-
onPanelChange日期面板变化时的回调function(value, mode)-

DatePicker[picker=year]#

参数说明类型默认值版本
defaultPickerValue默认面板日期moment-
defaultValue默认日期moment-
format展示的日期格式,配置参考 moment.jsstringYYYY
renderExtraFooter在面板中添加额外的页脚() => React.ReactNode-
value日期moment-
onChange时间发生变化的回调,发生在用户选择时间时function(date: moment, dateString: string)-

DatePicker[picker=quarter]#

4.1.0 新增。

参数说明类型默认值版本
defaultPickerValue默认面板日期moment-
defaultValue默认日期moment-
format展示的日期格式,配置参考 moment.jsstringYYYY-\QQ
renderExtraFooter在面板中添加额外的页脚() => React.ReactNode-
value日期moment-
onChange时间发生变化的回调,发生在用户选择时间时function(date: moment, dateString: string)-

DatePicker[picker=month]#

参数说明类型默认值版本
defaultPickerValue默认面板日期moment-
defaultValue默认日期moment-
format展示的日期格式,配置参考 moment.jsstringYYYY-MM
monthCellRender自定义的月份内容渲染方法function(date, locale): ReactNode-
renderExtraFooter在面板中添加额外的页脚() => React.ReactNode-
value日期moment-
onChange时间发生变化的回调,发生在用户选择时间时function(date: moment, dateString: string)-

DatePicker[picker=week]#

参数说明类型默认值版本
defaultPickerValue默认面板日期moment-
defaultValue默认日期moment-
format展示的日期格式,配置参考 moment.jsstringYYYY-wo
renderExtraFooter在面板中添加额外的页脚(mode) => React.ReactNode-
value日期moment-
onChange时间发生变化的回调,发生在用户选择时间时function(date: moment, dateString: string)-

RangePicker#

参数说明类型默认值版本
allowEmpty允许起始项部分为空[boolean, boolean][false, false]
dateRender自定义日期单元格的内容。info 参数自 4.3.0 添加function(currentDate: moment, today: moment, info: { range: start | end }) => React.ReactNode-
defaultPickerValue默认面板日期moment[]-
defaultValue默认日期moment[]-
disabled禁用起始项[boolean, boolean]-
disabledTime不可选择的时间function(date: moment, partial: start | end)-
format展示的日期格式stringYYYY-MM-DD HH:mm:ss
ranges预设时间范围快捷选择{ [range: string]: moment[] } | { [range: string]: () => moment[] }-
renderExtraFooter在面板中添加额外的页脚() => React.ReactNode-
separator设置分隔符React.ReactNode<SwapRightOutlined />
showTime增加时间选择功能Object|booleanTimePicker Options
showTime.defaultValue设置用户选择日期时默认的时分秒,例子moment[][moment(), moment()]
value日期moment[]-
onCalendarChange待选日期发生变化的回调。info 参数自 4.4.0 添加function(dates: [moment, moment], dateStrings: [string, string], info: { range:start|end })-
onChange日期范围发生变化的回调function(dates: [moment, moment], dateStrings: [string, string])-

FAQ#

当我指定了 DatePicker/RangePicker 的 mode 属性后,点击后无法选择年份/月份?#

请参考常见问答

如何在 DatePicker 中使用自定义日期库(如 dayjs )?#

请参考《替换 Moment.js》

为什么时间类组件的国际化 locale 设置不生效?#

参考 FAQ 为什么时间类组件的国际化 locale 设置不生效?

如何修改周的起始日?#

请使用正确的语言包#5605),或者修改 moment 的 locale 配置:https://codesandbox.io/s/moment-day-of-week-6dby5

moment.locale('en', {
  // 注意请修改你正在使用的 locale 语言,比如 zh-cn
  week: {
    dow: 1,
  },
});

为何使用 panelRender 时,原来面板无法切换?#

当你通过 panelRender 动态改变层级结构时,会使得原本的 Panel 被当做新的节点删除并创建。这使得其原本的状态会被重置,保持结构稳定即可。详情请参考 #27263

Checkbox多选框Form表单