Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent user from selecting date above end date in react-dates

I'm wondering how I could prevent a user from selecting dates above today's date. For example, today is 3.7 so let that be the highest end date a user could select.

<DateRangePicker
    startDate={this.state.startDate} 
    startDateId="startDate" 
    endDate={this.state.endDate} 
    endDateId="endDate" 
    onDatesChange={({ startDate, endDate }) => {
      this.setState({ startDate, endDate }, () => {});
    }} 
    focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
    onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
    daySize={50}
    noBorder={true}
    isOutsideRange={() => false}
/>
like image 241
Roxy'Pro Avatar asked Jul 03 '19 13:07

Roxy'Pro


People also ask

How do you turn off past and future dates in react datetime?

To disable the past and future dates, we have to use the isValidDate property of the react-datetime.

How to disable the dates in react-datetime?

To disable the dates, we have to use the isValidDate property of the react-datetime. As per the documentation, isValidDate is the function and by default is returning the true value. Define the dates that can be selected.

How to validate a date in ReactJS?

How to validate a Date in ReactJS? Data validation is an important step in every application in order to authenticate the user’s entered data which can be used to calculate the date of birth or any other use. It can be achieved using the validator module in ReactJS.

How do I select a date range in the calendar component?

One additional feature supported by the Calendar component is allowing the user to select a date range. By default, this functionality is disabled but we can enable it by setting the selectRange prop to true [1].

How to disable past dates in datepicker?

Check the following code to disable the past dates. // disable past dates const yesterday = moment().subtract(1, 'day'); const disablePastDt = current => { return current.isAfter(yesterday); }; return ( <DatePicker isValidDate={disablePastDt} /> );


Video Answer


2 Answers

You should use an isOutsideRange prop and Moment.js for working with available dates ranges. For example, you can allow selecting only dates within a past 30 days this way:

CodeSandbox

import React, { Component } from "react";
import moment from "moment";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";
import { DateRangePicker } from "react-dates";
import { START_DATE, END_DATE } from "react-dates/constants";

export default class Dates extends Component {
  state = {
    startDate: null,
    endDate: null,
    focusedInput: null
  };

  onDatesChange = ({ startDate, endDate }) =>
    this.setState({ startDate, endDate });

  onFocusChange = focusedInput => this.setState({ focusedInput });

  isOutsideRange = day =>
    day.isAfter(moment()) || day.isBefore(moment().subtract(30, "days"));

  render() {
    const { startDate, endDate, focusedInput } = this.state;

    return (
      <DateRangePicker
        startDate={startDate}
        startDateId={START_DATE}
        endDate={endDate}
        endDateId={END_DATE}
        onDatesChange={this.onDatesChange}
        focusedInput={focusedInput}
        onFocusChange={this.onFocusChange}
        daySize={50}
        noBorder={true}
        isOutsideRange={this.isOutsideRange}
      />
    );
  }
}

like image 186
sergdenisov Avatar answered Oct 23 '22 15:10

sergdenisov


I dont know if you found the solution. But I gave my solution anyway.

You can use import { isInclusivelyBeforeDay } from react-dates and use isOutsideRange={day => !isInclusivelyBeforeDay(day, moment())}

Hope it helps

like image 29
Phú Quốc Đinh Avatar answered Oct 23 '22 15:10

Phú Quốc Đinh