antd-mobile的DatePicker分钟精度半小时

项目要求,在时间选择上需要精确到分钟,且分钟只能半小时,既0分钟或者是30分钟。

前期引用的时间控件是antd-mobile的DatePicker组件,具体用法可参考:https://mobile.ant.design/components/date-picker-cn/

其中组件有个minuteStep参数,将其设置成30,即可只显示0分钟或者30分钟了。

但是在选择的时候发现了问题,点击时间控件,弹出时间选择的界面,如果不去选择0分钟或者30分钟,直接点击确认,控件会选择到当前时间的分钟数,这是不合理的,解决方法:

使用到了moment对象,需要在项目中引入moment.js。增加一个判断,如果选择到30分钟了,即不变。如果不是30分钟则将分钟数设置为0,具体做法如下:

1 <DatePicker value={this.state.startTime}  minuteStep = {30}
2    onChange={startTime =>  this.setState({startTime:  new Date(startTime).getMinutes() == 30 ? startTime :  moment( new Date(startTime).setMinutes(0) ) })}>
3   <List.Item arrow="horizontal"><font color="red"> * </font>开始时间</List.Item>
4 </DatePicker>
原文地址:https://www.cnblogs.com/sandyyeh/p/9207640.html