Search code examples
javascriptreactjsreact-datepicker

React datepicker set min and max date


I am using react-datepicker in my project.
I need to display only days for a specific month (lets say February 2020). I was under the impression that I could pass iso dates as minDate and maxDate but this does not seem to work.

My code:

   const DatePickerMod = () => {
    const [startDate, setStartDate] = useState(null);
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      minDate={'02-01-2020'}
      maxDate={'02-29-2020}
      placeholderText="Select a date in February 2020"
    />
  );
  };

Solution

  • pass your date (with that format) as string to a new Date() instance like so:

     <DatePicker
        selected={startDate}
        onChange={date => setStartDate(date)}
        minDate={new Date("02-01-2020")}
        maxDate={new Date("02-29-2020")}
        placeholderText="Select a date in February 2020"
    />