I am working on verifying the date, if it is present in between 2 dates, written following code to to validate dates. For few dates it is resulting invalid date even though the date is present in between the date range. Not sure what i am doing wrong here, anyone help me to resolve this.
Issue Dates : "2022-06-04", "2022-06-05", "2022-06-06", "2022-06-07", "2022-06-08", "2022-06-09"
Thank you
const dates = ["2022-05-01", "2022-05-02", "2022-05-03", "2022-05-04", "2022-05-05", "2022-05-06", "2022-05-07", "2022-05-08", "2022-05-09", "2022-05-10", "2022-06-01", "2022-06-02", "2022-06-03", "2022-06-04", "2022-06-05", "2022-06-06", "2022-06-07", "2022-06-08", "2022-06-09", "2022-06-10", "2022-06-11", "2022-06-12", "2022-06-13", "2022-06-14", "2022-06-15", "2022-06-16", "2022-06-17", "2022-06-18", "2022-06-19", "2022-06-20"];
dates.forEach(function (item) {
const recordDate = new Date(item).toLocaleDateString();
console.log(recordDate);
date1 = new Date("Sunday, May 3, 2022"); // from an input field
date2 = new Date("Thursday, June 20, 2022"); // from an input field
startDate = new Date(
date1.getFullYear(),
date1.getMonth(),
1
).toLocaleDateString(); // start of month from date1 '5/1/2022'
endDate = new Date(
date2.getFullYear(),
date2.getMonth() + 1,
0
).toLocaleDateString(); // end of month from date2 '6/30/2022'
if (recordDate >= startDate && recordDate <= endDate) {
console.log("valid");
} else console.log("not valid");
});