2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 6
|
2016-06-19 11:17:09 +08:00
|
|
|
title:
|
2016-11-11 12:08:18 +08:00
|
|
|
zh-CN: 不可选择日期和时间
|
|
|
|
en-US: Disabled Date & Time
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-12-25 01:21:03 +08:00
|
|
|
|
2016-06-19 11:17:09 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-11-11 12:08:18 +08:00
|
|
|
可用 `disabledDate` 和 `disabledTime` 分别禁止选择部分日期和时间,其中 `disabledTime` 需要和 `showTime` 一起使用。
|
2015-12-25 01:21:03 +08:00
|
|
|
|
2016-06-19 11:17:09 +08:00
|
|
|
## en-US
|
|
|
|
|
2016-11-11 12:08:18 +08:00
|
|
|
Disabled part of dates and time by `disabledDate` and `disabledTime` respectively, and `disabledTime` only works with `showTime`.
|
2016-06-19 11:17:09 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2017-05-18 14:51:06 +08:00
|
|
|
import moment from 'moment';
|
2020-07-30 19:17:34 +08:00
|
|
|
import { DatePicker, Space } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
2016-10-25 17:39:17 +08:00
|
|
|
|
2016-11-11 12:08:18 +08:00
|
|
|
function range(start, end) {
|
2016-10-25 17:39:17 +08:00
|
|
|
const result = [];
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
result.push(i);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:08:18 +08:00
|
|
|
function disabledDate(current) {
|
2017-05-18 14:51:06 +08:00
|
|
|
// Can not select days before today and today
|
2018-01-08 20:31:49 +08:00
|
|
|
return current && current < moment().endOf('day');
|
2016-11-11 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function disabledDateTime() {
|
|
|
|
return {
|
2016-12-07 10:05:31 +08:00
|
|
|
disabledHours: () => range(0, 24).splice(4, 20),
|
2016-11-14 19:13:37 +08:00
|
|
|
disabledMinutes: () => range(30, 60),
|
|
|
|
disabledSeconds: () => [55, 56],
|
2016-11-11 12:08:18 +08:00
|
|
|
};
|
|
|
|
}
|
2015-12-25 01:21:03 +08:00
|
|
|
|
2016-11-11 12:08:18 +08:00
|
|
|
function disabledRangeTime(_, type) {
|
2016-10-25 17:39:17 +08:00
|
|
|
if (type === 'start') {
|
|
|
|
return {
|
2016-11-14 19:13:37 +08:00
|
|
|
disabledHours: () => range(0, 60).splice(4, 20),
|
|
|
|
disabledMinutes: () => range(30, 60),
|
|
|
|
disabledSeconds: () => [55, 56],
|
2016-10-25 17:39:17 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
2016-11-14 19:13:37 +08:00
|
|
|
disabledHours: () => range(0, 60).splice(20, 4),
|
|
|
|
disabledMinutes: () => range(0, 31),
|
|
|
|
disabledSeconds: () => [55, 56],
|
2016-10-25 17:39:17 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => (
|
2020-07-30 19:17:34 +08:00
|
|
|
<Space direction="vertical" size={12}>
|
2017-01-19 13:55:04 +08:00
|
|
|
<DatePicker
|
|
|
|
format="YYYY-MM-DD HH:mm:ss"
|
|
|
|
disabledDate={disabledDate}
|
|
|
|
disabledTime={disabledDateTime}
|
2017-05-18 14:51:06 +08:00
|
|
|
showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
|
2017-01-19 13:55:04 +08:00
|
|
|
/>
|
2019-12-11 23:32:19 +08:00
|
|
|
<DatePicker picker="month" disabledDate={disabledDate} />
|
2020-03-18 16:59:15 +08:00
|
|
|
<RangePicker disabledDate={disabledDate} />
|
2017-01-19 13:55:04 +08:00
|
|
|
<RangePicker
|
|
|
|
disabledDate={disabledDate}
|
|
|
|
disabledTime={disabledRangeTime}
|
2017-05-18 14:51:06 +08:00
|
|
|
showTime={{
|
|
|
|
hideDisabledOptions: true,
|
|
|
|
defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('11:59:59', 'HH:mm:ss')],
|
|
|
|
}}
|
2017-01-19 13:55:04 +08:00
|
|
|
format="YYYY-MM-DD HH:mm:ss"
|
|
|
|
/>
|
2022-04-03 23:27:45 +08:00
|
|
|
</Space>
|
2016-11-14 11:56:05 +08:00
|
|
|
);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|