ant-design/components/date-picker/demo/disabled-date.md

82 lines
1.9 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 6
2016-06-19 11:17:09 +08:00
title:
zh-CN: 不可选择日期和时间
en-US: Disabled Date & Time
2016-03-31 09:40:55 +08:00
---
2016-06-19 11:17:09 +08:00
## zh-CN
可用 `disabledDate``disabledTime` 分别禁止选择部分日期和时间,其中 `disabledTime` 需要和 `showTime` 一起使用。
2016-06-19 11:17:09 +08:00
## en-US
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
2017-02-13 10:55:53 +08:00
````jsx
import moment from 'moment';
import { DatePicker } from 'antd';
2018-06-27 15:55:04 +08:00
2017-05-15 16:18:08 +08:00
const { MonthPicker, RangePicker } = DatePicker;
function range(start, end) {
const result = [];
for (let i = start; i < end; i++) {
result.push(i);
}
return result;
}
function disabledDate(current) {
// Can not select days before today and today
return current && current < moment().endOf('day');
}
function disabledDateTime() {
return {
disabledHours: () => range(0, 24).splice(4, 20),
2016-11-14 19:13:37 +08:00
disabledMinutes: () => range(30, 60),
disabledSeconds: () => [55, 56],
};
}
function disabledRangeTime(_, type) {
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],
};
}
return {
2016-11-14 19:13:37 +08:00
disabledHours: () => range(0, 60).splice(20, 4),
disabledMinutes: () => range(0, 31),
disabledSeconds: () => [55, 56],
};
}
ReactDOM.render(
<div>
<DatePicker
format="YYYY-MM-DD HH:mm:ss"
disabledDate={disabledDate}
disabledTime={disabledDateTime}
showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
/>
<br />
2017-05-15 16:18:08 +08:00
<MonthPicker disabledDate={disabledDate} placeholder="Select month" />
<br />
<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"
/>
</div>,
mountNode
);
````