How can I limit the selection dates between today’s date and 7 days back only. How can I achieve this limitation?
<DateTimePicker testID="dateTimePicker" timeZoneOffsetInMinutes={0} value={date} mode={date} is24Hour={true} display="default" onChange={onChange} maximumDate={new Date(2021, 2, 11)} minimumDate={new Date(2021, 2, 4)} />
Advertisement
Answer
Well, as you wrote, you have two to properties named maximumDate
and minimumDate
.
To get what you need you have to subtract, to the current date, seven days and put the current date as the maximum and the date you get before as the minimum.
To get the current, create a new instance of a Date
in this way:
new Date();
In this way the instance is initialized with the current date, based on the operating system date.
To get a date that is seven days before the current date, you can do in this way:
const lastWeek = new Date(); lastWeek.setDate(lastWeek.getDate() - 7);
So, the last thing to do is use these values:
<DateTimePicker maximumDate={new Date()} minimumDate={lastWeek} />