简体   繁体   English

如何在TimePickerAndroid中为React-Native设置最小日期

[英]How to set min date in TimePickerAndroid for react-native

I created a simple form with react-native using this library with start time and end time. 我使用这个带有开始时间和结束时间的创建了一个带有react-native的简单表单。 I was asking my self why I can set with no problem the start time and end time on ios, but I cannot set it in android when I found in the official documentation that it isn't expected the min time option. 我在问自己为什么我可以毫无问题地在ios上设置开始时间和结束时间,但是当我在官方文档中发现它不是最短时间选项时,就无法在android中进行设置。 Does exist any workaround or some hack to solve this problem? 是否存在任何解决方法或某种破解方法来解决此问题?
The code is so similar to the one in the example so I didn't provide it, but in the case I can edit the message. 该代码与示例中的代码非常相似,因此我没有提供它,但是在这种情况下,我可以编辑消息。 Thanks 谢谢

According to the documentation, for the react-native-datepicker , minDate is not supported for the Android. 根据文档,对于react-native-datepicker ,Android不支持minDate。 It works fine for ios. 它适用于ios。

Note : Use the "datetime" value for the prop -mode. 注意:将“ datetime”值用于prop -mode。

I followed the method below to overcome the issue in android. 我遵循以下方法来克服android中的问题。

                    <DatePicker
                      style={{width: 200}}
                      date={this.state.valueDate}
                      mode="datetime"
                      format="YYYY/MM/DD HH:mm"
                      minDate={new Date(Date.now()+(10 * 60 * 1000))}   //To book after 10 minutes
                      confirmBtnText="Confirm"
                      cancelBtnText="Cancel"
                      customStyles={{
                        dateIcon: {
                          position: 'absolute',
                          left: 5,
                          top: 4,
                          marginLeft: 10
                        },
                        dateInput: {
                          marginLeft: 46
                        }
                        // ... You can check the source to find the other keys.
                      }}
                      onDateChange={(date) => {   // returns 2019-10-15T23:23 - set in format prop
                        var selectedDate = new Date(date);
                        var dateNow = new Date();
                        var YYYY = dateNow.getFullYear();
                        var MM = dateNow.getMonth()+1;
                        var DD = dateNow.getDate();
                        var HH = dateNow.getHours();
                        var mm = dateNow.getMinutes()+10;   //To book after 10 minutes
                        var minDate = new Date(YYYY+'/'+MM+'/'+DD+' '+HH+':'+mm)

                        var selectedYYYY = selectedDate.getUTCFullYear();
                        var selectedMM = selectedDate.getUTCMonth()+1;
                        var selectedDD = selectedDate.getUTCDate();
                        var selectedHH = selectedDate. getUTCHours();
                        var selectedmm = selectedDate.getUTCMinutes();

                        if(Platform.OS == 'ios'){
                          this.setState({
                            valueDate:date,
                            date: selectedYYYY+'-'+selectedMM+'-'+selectedDD,
                            time: selectedHH+':'+selectedmm
                          })
                        }else{
                          if(selectedDate.getTime()<minDate.getTime()){   //getTime() - Get the time (milliseconds since January 1, 1970)
                            alert("You can book after 10 minutes from now!");
                          }else{
                            this.setState({
                              valueDate:date,
                              date: selectedYYYY+'-'+selectedMM+'-'+selectedDD,
                              time: selectedHH+':'+selectedmm
                            })
                          }
                        }

                      }
                        }

                    />

Hope it will help!! 希望对您有所帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM