2016-11-11 12:08:18 +08:00
|
|
|
---
|
2019-12-11 23:32:19 +08:00
|
|
|
order: 2
|
2016-11-11 12:08:18 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 日期格式
|
|
|
|
en-US: Date Format
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 `format` 属性,可以自定义日期显示格式。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
We can set the date format by `format`.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
|
|
|
import type { DatePickerProps } from 'antd';
|
2020-07-30 19:17:34 +08:00
|
|
|
import { DatePicker, Space } from 'antd';
|
2022-02-14 14:40:40 +08:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
2022-05-21 22:14:15 +08:00
|
|
|
import React from 'react';
|
2022-02-14 14:40:40 +08:00
|
|
|
|
|
|
|
dayjs.extend(customParseFormat);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
2016-11-11 12:08:18 +08:00
|
|
|
|
|
|
|
const dateFormat = 'YYYY/MM/DD';
|
2021-09-14 18:39:44 +08:00
|
|
|
const weekFormat = 'MM/DD';
|
2016-11-11 12:08:18 +08:00
|
|
|
const monthFormat = 'YYYY/MM';
|
2019-03-01 13:50:28 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
const dateFormatList = ['DD/MM/YYYY', 'DD/MM/YY'];
|
2019-03-01 13:50:28 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const customFormat: DatePickerProps['format'] = value =>
|
|
|
|
`custom format: ${value.format(dateFormat)}`;
|
2020-09-22 15:43:11 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const customWeekStartEndFormat: DatePickerProps['format'] = value =>
|
2022-02-14 14:40:40 +08:00
|
|
|
`${dayjs(value).startOf('week').format(weekFormat)} ~ ${dayjs(value)
|
2021-09-14 18:39:44 +08:00
|
|
|
.endOf('week')
|
|
|
|
.format(weekFormat)}`;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => (
|
2020-07-30 19:17:34 +08:00
|
|
|
<Space direction="vertical" size={12}>
|
2022-02-14 14:40:40 +08:00
|
|
|
<DatePicker defaultValue={dayjs('2015/01/01', dateFormat)} format={dateFormat} />
|
|
|
|
<DatePicker defaultValue={dayjs('01/01/2015', dateFormatList[0])} format={dateFormatList} />
|
|
|
|
<DatePicker defaultValue={dayjs('2015/01', monthFormat)} format={monthFormat} picker="month" />
|
|
|
|
<DatePicker defaultValue={dayjs()} format={customWeekStartEndFormat} picker="week" />
|
2016-11-11 12:08:18 +08:00
|
|
|
<RangePicker
|
2022-02-14 14:40:40 +08:00
|
|
|
defaultValue={[dayjs('2015/01/01', dateFormat), dayjs('2015/01/01', dateFormat)]}
|
2016-11-11 12:08:18 +08:00
|
|
|
format={dateFormat}
|
2016-11-14 19:13:37 +08:00
|
|
|
/>
|
2022-02-14 14:40:40 +08:00
|
|
|
<DatePicker defaultValue={dayjs('2015/01/01', dateFormat)} format={customFormat} />
|
2022-04-03 23:27:45 +08:00
|
|
|
</Space>
|
2018-11-28 15:00:03 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|