The initial state of the startDate and endDate hooks represents a date range starting from today and ending 7 days from today.
Calculation:
startDate:
- The
startDateis initialised to today’s date, with the time component set to00:00:00. - It uses the current year, month, and day obtained from
new Date().
endDate
- The
endDateis initialised to a date 7 days after today, also with the time component set to00:00:00. - It adds 7 to the day of the current date while keeping the year and month the same.
Code:
const [startDate, setStartDate] = useState<Date | null>(() => {
const today = new Date();
return new Date(today.getFullYear(), today.getMonth(), today.getDate()); // Start at today
});
const [endDate, setEndDate] = useState<Date | null>(() => {
const today = new Date();
const sevenDaysLater = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7);
return sevenDaysLater; // End 7 days from today
});