antd-mobile的DatePicker日期选择组件使用

如今项目上有个需求,在时间选择上须要精确到分钟,且分钟只能是0分钟或者是30分钟。antd

使用了antd-mobile的DatePicker组件,具体用法可参考:https://mobile.ant.design/components/date-picker-cn/this

 

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

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

参考了https://www.zhihu.com/question/56076235,使用到了moment对象,须要在项目中引入moment.js。增长一个判断,若是选择到30分钟了,即不变。若是不是30分钟则将分钟数设置为0,具体作法以下:component

 

                <DatePicker
                    value={this.state.startTime}  minuteStep = {30}
                    onChange={startTime =>  this.setState({startTime:  new Date(startTime).getMinutes() == 30 ? startTime :  moment( new Date(startTime).setMinutes(0) ) })}
                    >
                <List.Item arrow="horizontal"><font color="red"> * </font>开始时间</List.Item>
                </DatePicker>        

 

这样既可实现。对象

相关文章
相关标签/搜索