項(xiàng)目中用到的,需要限制時(shí)間控件的可選范圍或详,以下代碼為當(dāng)前時(shí)間(到秒)之后的時(shí)間為可選時(shí)間郭计。
range = (start, end) => {
const result = [];
for (let i = start; i <= end; i++) {
result.push(i);
}
console.log(result);
return result;
};
disabledDate = (current) => {
// 不能選今天和今天之前的日期
return current && current < moment();
};
disabledDateTime = () => {
let hours = moment().hours();//0~23
let minutes = moment().minutes();//0~59
//當(dāng)日只能選擇當(dāng)前時(shí)間之后的時(shí)間點(diǎn)
if (this.state.upgradeTime.date() === moment().date()) {
return {
disabledHours: () => this.range(0, hours),
disabledMinutes: () => this.range(0, minutes),
};
}
};
<DatePicker
onChange={(upgradeTime) => this.setState({upgradeTime})}
showTime={{defaultValue: moment(this.state.upgradeTime)}}
disabledDate={this.disabledDate}
disabledTime={this.disabledDateTime}
format="YYYY-MM-DD HH:mm:ss"
placeholder="選擇時(shí)間"
/>