4

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"
    />
  );
  };
greatTeacherOnizuka
  • 555
  • 1
  • 6
  • 24

4 Answers4

8

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"
/>
zb22
  • 3,126
  • 3
  • 19
  • 34
2

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

Marko Savic
  • 2,159
  • 2
  • 14
  • 27
0

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

Nawin
  • 485
  • 8
  • 14
0

If you're using the SingleDatePicker component instead of the DateRangePicker component, you will need to use isOutsideRange prop as minDate and maxDate don't exist on SingleDatePicker.

<SingleDatePicker
  standardProps...
  isOutsideRange={(day) => {
    if (minDate) return minDate > day ? true : false;
    if (maxDate) return maxDate < day ? true : false;
    return false;
  }}
/>
yeoman
  • 359
  • 2
  • 6