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}
/>
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}
/>
Share
Improve this question
edited Oct 13, 2021 at 17:01
TylerH
21.1k77 gold badges79 silver badges112 bronze badges
asked Jul 3, 2019 at 13:17
Roxy'ProRoxy'Pro
4,4549 gold badges49 silver badges120 bronze badges
0
5 Answers
Reset to default 6You 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}
/>
);
}
}
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
I had a similar issue on another react date picker package. After a bit of reading the documentation for yours (the AirBnb one) I was able to find this issue being mentionned on their GitHub: Set date range #86
It appears there is a prop called isOutsideRange
that takes a function. You could return false
for any date outside of the current date for example, with just a parison.
Hope it helps
function
isOutsideRange(day) {
return (moment().diff(day) < 0);
}
...
<DateRangePicker
isOutsideRange={this.isOutsideRange}
>
In the latest date range picker version, I just used maxDate={new Date()}
. This worked fine to me. This didn't allow me to select more than today.