import { DatePicker } from 'antd';
import moment from 'moment';
const range = (start, end) => {
const result = [];
for (let i = start; i < end; i++) {
result.push(i);
}
return result;
}
const disabledTime = date => {
const hour = moment().hours(); // 获取当前的小时
const min = moment().minutes(); // 获取当前的分钟
const second = moment().seconds(); // 获取当前秒
if (date?.format('YYYY-MM-DD').valueof() === moment().format('YYYY-MM-DD').valueof()) {
if (date?.format('HH').valueof() === moment().format('HH').valueof()) {
if (date?.format('mm').valueof() === moment).format('mm').valueof()) {
return {
disabledHours: () => range(0, 24).splice(0, hour),
disabledMinutes: () => range(0, 60).splice(0, min),
disabledSeconds: () => range(0, 60).splice(0, second)
};
}
else {
return {
disabledHours: () => range(0, 24).splice(0, hour),
disabledMinutes: () => range(0, 60).splice(0, min)
};
}
}
else {
return {
disabledHours: () => range(0, 24).splice(0, hour)
};
}
}
};
<DatePicker
showTime
disabledDate={cur => cur && cur < moment().startof('day')}
disabledTime={disabledTime}
>
disabledDate={cur => cur && cur < moment().startof('day')} 限制了日期选择只能选择今天或者之后的日期
disabledTime={disabledTime} 调用函数,限制可以选择的时间为当前时间之后的时间,仅限制了当天的时分秒,之后的并未限制。
可根据实际情况进行限制,本例子只是以当天和当前时间的时分秒为限制