reactjsreact-nativereact-datepickerreact-datesreact-datetime

How to Disable Future Year, Month and Date in RN?


I know there a lots same questions, but none of them work as I need, about to disable future YYYY-MM-DD in React Native Date Picker:

    <DatePicker
      modal
      open={open}
      date={date}
      mode="date"
      onConfirm={updateFilter}
      maximumDate={new Date()}
      onCancel={() => {
        setOpen(false)
      }}
      
    />

But it doesn't work, the future month and date still displayed, only future year is disabled. How to disable all of them?


Solution

  • According to the Readme on the repo for react-native-date-picker, the maximumDate prop requires a string in the form YYYY-MM-DD. Therefore you should instantiate a new Date object and store it in a variable on which you can call the various methods you need to access those portions of the Date. Then you can pass that prop a new Date object, and add the pieces of the string you need, like so:

    const currDate = new Date();
    <DatePicker>
        ...
        maximumDate={new Date(`${currDate.getFullYear()}-${currDate.getMonth() + 1}-${currDate.getDate()}`)}
    </DatePicker>