ant-design/components/date-picker/demo/presetted-ranges.md

56 lines
1.2 KiB
Markdown
Raw Normal View History

---
order: 8
title:
2016-11-15 17:16:20 +08:00
zh-CN: 预设范围
en-US: Preset Ranges
---
## zh-CN
可以预设常用的日期范围以提高用户体验。
## en-US
We can set preset ranges to RangePicker to improve user experience.
```tsx
import { DatePicker, Space } from 'antd';
import type { RangePickerProps } from 'antd/es/date-picker';
import dayjs from 'dayjs';
2022-05-21 22:14:15 +08:00
import React from 'react';
2018-06-27 15:55:04 +08:00
const { RangePicker } = DatePicker;
const onChange: RangePickerProps['onChange'] = (dates, dateStrings) => {
2022-05-18 17:25:40 +08:00
if (dates) {
console.log('From: ', dates[0], ', to: ', dates[1]);
console.log('From: ', dateStrings[0], ', to: ', dateStrings[1]);
} else {
console.log('Clear');
}
};
2017-05-15 16:36:07 +08:00
const App: React.FC = () => (
<Space direction="vertical" size={12}>
<RangePicker
2019-05-07 14:57:32 +08:00
ranges={{
Today: [dayjs(), dayjs()],
'This Month': [dayjs().startOf('month'), dayjs().endOf('month')],
2019-05-07 14:57:32 +08:00
}}
2016-11-15 17:16:20 +08:00
onChange={onChange}
/>
<RangePicker
2019-05-07 14:57:32 +08:00
ranges={{
Today: [dayjs(), dayjs()],
'This Month': [dayjs().startOf('month'), dayjs().endOf('month')],
2019-05-07 14:57:32 +08:00
}}
2017-05-15 14:37:22 +08:00
showTime
format="YYYY/MM/DD HH:mm:ss"
onChange={onChange}
/>
</Space>
);
export default App;
2019-05-07 14:57:32 +08:00
```