Search code examples
reactjsreact-datepicker

Exclude future dates from a given date using React Datepicker


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?


Solution

  • 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"
    />