React+Ant Design组件,使用表单组件时,采用日期选择器,涉及到时间分钟秒,第一次使用不是很会,平常都是直接用的组件,直接CV即可,现在的需求需要做一下限制,现在当天的时间,百度一些文档,发现都是只有一种情况,要么就是特别复杂的,小研究了一下,发现一行代码就搞定了,记录下,方便以后自己开发使用。
限制用户在选择时间的时候,只能选择当天用户的时间段:
disabledDate = (current) => {
return current <=moment().subtract(1, "days") || current >= moment().endOf('day');
};
<Form.Item
label="时间"
labelCol={{ span: 3, offset: 1 }}
name="time"
rules={[{ required: true, message: '请选择时间' }]}
>
<RangePicker
showTime
disabledDate={this.disabledDate}
format="YYYY-MM-DD HH:mm:ss"
onChange={this.getDateTime}
/>
</Form.Item>