- Ant Design of React
- Changelogv6.1.3
- Basic Usage
- AI
- Advanced
- Migration
- Other
By default, Ant Design uses Day.js to handle time and date. Day.js is an immutable date-time library alternative to Moment.js with the same API.
You might want to use another date library (Ant design currently supports moment, date-fns, and luxon ). We provide two ways to customize:
The first way is to use generatePicker (or generateCalendar) to help create Picker components.
First, we initialize an antd demo. You can refer to Scaffolding Guide, or you can start directly here init antd
Create src/components/DatePicker.tsx.
For example:
importmomentGenerateConfigfrom'@rc-component/picker/generate/moment';import{DatePicker}from'antd';importtype{Moment}from'moment';constMyDatePicker=DatePicker.generatePicker<Moment>(momentGenerateConfig);exportdefaultMyDatePicker;
Create src/components/TimePicker.tsx.
For example:
import*asReactfrom'react';importtype{PickerTimeProps}from'antd/es/date-picker/generatePicker';importtype{Moment}from'moment';importDatePickerfrom'./DatePicker';exportinterfaceTimePickerPropsextendsOmit<PickerTimeProps<Moment>,'picker'>{}constTimePicker=React.forwardRef<any,TimePickerProps>((props, ref)=>(<DatePicker{...props}picker="time"mode={undefined}ref={ref}/>));TimePicker.displayName='TimePicker';exportdefaultTimePicker;
Create src/components/Calendar.tsx.
For example:
importmomentGenerateConfigfrom'@rc-component/picker/generate/moment';import{Calendar}from'antd';importtype{Moment}from'moment';constMyCalendar=Calendar.generateCalendar<Moment>(momentGenerateConfig);exportdefaultMyCalendar;
Create src/components/index.tsx.
For example:
export{defaultasCalendar}from'./Calendar';export{defaultasDatePicker}from'./DatePicker';export{defaultasTimePicker}from'./TimePicker';
Modify src/App.tsx,import moment and custom component.
- import { DatePicker, Calendar } from 'antd';- import format from 'dayjs';+ import { DatePicker, TimePicker, Calendar } from './components';+ import format from 'moment';
We also provide another implementation, which we provide with @ant-design/moment-webpack-plugin, replacing Day.js with moment directly without changing a line of existing code. More info can be found at @ant-design/moment-webpack-plugin.
// webpack-config.jsconstAntdMomentWebpackPlugin=require('@ant-design/moment-webpack-plugin');module.exports={// ...plugins:[newAntdMomentWebpackPlugin()],};
date-fns currently supports custom component methods similar to dayjs. The difference is that the parameter types used are different. Support is provided in antd 4.5.0 and above.
For Example:
Create src/components/DatePicker.tsx.
Code as follows:
importdateFnsGenerateConfigfrom'@rc-component/picker/generate/dateFns';import{DatePicker}from'antd';constMyDatePicker=DatePicker.generatePicker<Date>(dateFnsGenerateConfig);exportdefaultMyDatePicker;
Since antd 5.4.0, luxon can be used instead of dayjs and supports the same functionality, but it does introduce some differences in behavior that we will explain below.
Create a src/components/DatePicker.tsx file, and implement the luxon based picker as follows:
importluxonGenerateConfigfrom'@rc-component/picker/generate/luxon';import{DatePicker}from'antd';importtype{DateTime}from'luxon';constMyDatePicker=DatePicker.generatePicker<DateTime>(luxonGenerateConfig);exportdefaultMyDatePicker;
luxon users should be familiar with the fact that it does not come with a custom implementation for localization. Instead, it relies on the browser's native Intl API.
This introduces some formatting differences with the other date libraries. As of today, the main differences are:
It is possible to customize these default luxon behaviors by adjusting the luxon config:
importluxonGenerateConfigfrom'@rc-component/picker/generate/luxon';import{DatePicker}from'antd';importtype{DateTime}from'luxon';const customLuxonConfig ={...luxonGenerateConfig,getWeekFirstDay(locale){// Your custom implementation goes here},};constMyDatePicker=DatePicker.generatePicker<DateTime>(customLuxonConfig);exportdefaultMyDatePicker;
Note that by doing such customization, the resulting DatePicker behavior might be altered in unexpected ways, so make sure you are testing for edge cases.