const [date, setDate] = useState(new Date());
const [openToDatePicker, setOpenToDatePicker] = useState(false);
const [openFromDatePicker, setOpenFromDatePicker] = useState(false);
const [fromDate, setFromDate] = useState('');
const [toDate, setToDate] = useState('');
const pastDate = () => {
const today = new Date();
if (toDate) {
toDate > fromDate;
}
const oneMonthBefore = new Date(today);
oneMonthBefore.setMonth(today.getMonth() - 1);
return oneMonthBefore;
};
<DatePicker
modal
open={openFromDatePicker || openToDatePicker}
date={date}
maximumDate={
new Date(
${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
)
}
minimumDate={pastDate()}
mode=”date”
onConfirm={(selectedDate: any) => {
if (openFromDatePicker) {
setOpenFromDatePicker(false);
setFromDate(selectedDate);
setFromDateFilter(true);
} else {
setOpenToDatePicker(false);
setToDate(selectedDate);
setToDateFilter(true);
}
}}
onCancel={() => {
setOpenFromDatePicker(false);
setOpenToDatePicker(false);
}}
/>
In this problem, there are four scenarios:
-
First Scenario: User can select up to one month ago date from the current date. (NOTE: This is already done).
-
Second Scenario: User can’t select a future date, meaning the user is eligible to select dates up to the current date. (NOTE: This is already done).
-
Third Scenario: If the user selects “fromDate”, then I have to restrict the user so that they can’t select “toDate” lower than fromDate. (For example, if the user selects “fromDate” as 14/06/24, the user must select “toDate” higher than or equal to 14/06/24).
-
Fourth Scenario: If the user selects “toDate”, then I have to restrict the user so that they can’t select “fromDate” higher than toDate. (For example, if the user selects “toDate” as 16/06/24, the user must select “fromDate” lower than or equal to 16/06/24).
However, I am encountering the following error: “Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.”
Shubh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.