I have the following helper function I created, which is to serve to create an array of a date range. Assume that the holidays variable is an array of ISO date strings and is already populated elsewhere in my vue app.
export const parseAvailableDates = (
startDate,
endDate,
holidays
) => {
console.log({ startDate, endDate, holidays });
let dates = [];
for (
let i = new Date(startDate);
i <= new Date(endDate);
i.setDate(i.getDate() + 1)
) {
console.log("running");
const thisDate = i.toLocaleString("en-US", localeString);
if (!thisDate.includes("Sunday") && !thisDate.includes("Saturday")) {
const formattedDate = changeLocaleDateToISOString(thisDate);
if (!holidays.includes(formattedDate)) {
dates.push(formattedDate);
}
}
}
console.log(dates);
return dates;
};
Now, I need to call this function twice in my Vue app in the same function, once to initially populate the dates based on the range between startAssignments and lastDateToSubmitWork, and again to reset the availableDates array to the range between startAssignments and endDate.
// called here
const startAssignments = `2022-02-09T00:00:00.000+04:00`
const lastDateToSubmitWork = `2022-08-19T00:00:00.000+04:00`
const endDate = `2022-08-31T00:00:00.000+04:00`
let availableDates = [];
availableDates = parseAvailableDates(
startAssignments,
lastDateToSubmitWork,
holidays
);
console.log(availableDates);
// also called here
availableDates = parseAvailableDates(
startAssignments,
endDate,
holidays
);
When these two calls happens, the initial console.log() in parseAvailableDates() shows the startDate, endDate, and holidays properly defined on both calls. However, on the first call, the for loop never runs, and the function returns [], and on the second call, the for loop does run, and the function returns a properly populated array of ISO date strings.
I don't understand why the first loop doesn't run properly, but the second does.