組件:
{form.getFieldValue('status') === '03' &&
getFieldDecorator('timingPublishTime', {
initialValue:
isEdit && infomentDetail.timingPublishTime
? moment(infomentDetail.timingPublishTime)
: moment(),
rules: [{ required: true, message: '請選擇定時發布時間' }]
})(
<DatePicker
format="YYYY-MM-DD HH:mm:ss"
disabledDate={disabledDate}
showTime
disabledTime={disabledTime}
/>
)}
方法:
const range = (start: any, end: any) => {
const result = [];
for (let i = start; i < end; i += 1) {
result.push(i);
}
return result;
};
// 不可選擇以前的時間
const disabledDate = (currentDate: any) => currentDate && currentDate < moment();
const disabledTime = (date: any) => {
const hours = moment().hours();
const minutes = moment().minutes();
const seconds = moment().seconds();
// 當日只能選擇當前時間之后的時間點
if (date && moment(date).date() === moment().date()) {
return {
disabledHours: () => range(0, 24).splice(0, hours),
disabledMinutes: () => range(0, 60).splice(0, minutes + 1),
disabledSeconds: () => range(0, 60).splice(0, seconds + 1)
};
}
return {
disabledHours: () => [],
disabledMinutes: () => [],
disabledSeconds: () => []
};
};
