React-native-modal-datetime-picker: How Can I disable Current Dates and Future Dates?

Created on 8 Jun 2017  路  3Comments  路  Source: mmazzarolo/react-native-modal-datetime-picker

I am using this Module But How Can I Disable Future and Current Dates...

_handleDatePicked = (date) => {
    var DateFormat =  moment(date).format("YYYY-MM-DD");
     console.log('A date has been picked:'+DateFormat);

     var d1 =new Date();
    d1.setHours(0,0,0,0)
     var d = new Date(DateFormat);
     d.setHours(0,0,0,0)
    var n = d.getDate();
    var m = d1.getDate();

    if(m < n){
        Alert.alert("Hello", 'Current Date and Future Dates Not allowing')  
        console.log("n:"+n +":"+"m:"+m) 
        }
        else{
           this.setState({
          DateOfBirth:DateFormat
         })
       } 
     this._hideDateTimePicker();
   };

This Code Is Not Allowing Future Dates But Allowing CurrentDate , I want Disable Current Date Also

Most helpful comment

To block future dates initialize your DateTimePicker with maximumDate prop likes so:

<DateTimePicker
   isVisible={this.state.isDateTimePickerVisible}
   maximumDate={new Date()}
   onConfirm={this.handleDatePicked}
   onCancel={this.hideDateTimePicker}
/>

To block current date use maximumDate like so:

<DateTimePicker
   isVisible={this.state.isDateTimePickerVisible}
   onConfirm={this.handleDatePicked}
   maximumDate={new Date(Date.now() - 86400000)}
   onCancel={this.hideDateTimePicker}
/>

All 3 comments

Hey @lavarajallu, sorry for answering so late. Are you still having the issue?
I'm on vacation right now, I'll take a look at it when I'll be back.

how can you resolve this issue?

To block future dates initialize your DateTimePicker with maximumDate prop likes so:

<DateTimePicker
   isVisible={this.state.isDateTimePickerVisible}
   maximumDate={new Date()}
   onConfirm={this.handleDatePicked}
   onCancel={this.hideDateTimePicker}
/>

To block current date use maximumDate like so:

<DateTimePicker
   isVisible={this.state.isDateTimePickerVisible}
   onConfirm={this.handleDatePicked}
   maximumDate={new Date(Date.now() - 86400000)}
   onCancel={this.hideDateTimePicker}
/>
Was this page helpful?
0 / 5 - 0 ratings