3
votes

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"
    />
  );
  };
3

3 Answers

6
votes

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"
/>
2
votes

minDate and maxDate are Date objects not String.

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

MDN Date

React DatePicker Documentation

0
votes

If you just want to use input type date field follow this

In your render use like this.

<input type="date" name="DOB"  min={minDate} max={today} />

and declare some variable in js like this

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0 so need to add 1 to make it 1!
var yyyy = today.getFullYear();

if (dd < 10) {
    dd = '0' + dd
}

if (mm < 10) {
    mm = '0' + mm
}

today = yyyy + '-' + mm + '-' + dd;
var minDate = "1993-05-25"

This will help you to avoid any external installations