7

When using the React Datepicker library with Moment.js to manipulate dates, one can exclude given days as captured below and described in the React Datepicker documentation;

// Exclude today and yesterday.
excludeDates = {[moment(), moment().subtract(1, "days")]}

How can I exclude future dates from a given date?

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Lizz Parody
  • 1,705
  • 11
  • 29
  • 48

2 Answers2

18

You can use filterDate check and return true if the date is in past.

The snippet below illustrates how to use filterDate;

<DatePicker
  selected = {this.state.date}
  onChange = {this.handleChange}
  filterDate = {(date) => {
    return moment() > date;
  }}
  placeholderText = "Select a weekday"
/>
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • I've used the following to disable future dates (from today onwards): filterDate={date => { return new Date() > date; }} – call0fcode Jun 22 '22 at 19:06
0

You can use Datepicker's minDate & maxDate options.

Eg: if you want to exclude all the dates after 10 days,

<DatePicker
selected={new Date()}
onChange={this.handleChange}
maxDate={addDays(new Date(), 10)}
/>

If you want to exclude all past dates and dates after 10 days.

<DatePicker
selected={new Date()}
onChange={this.handleChange}
minDate={new Date()}
maxDate={addDays(new Date(), 10)}
/>
Saji Xavier
  • 2,132
  • 16
  • 21