I have a date range
Example Date Range:
const startDate = "2022-06-02";
const endDate = "2022-06-20";
I want to get those dates which comes on provided days array that falls between startDate & endDate
Example Array of Days:
["tuesday", "friday", "saturday"]
The expected result is :
2022-06-03
2022-06-04
2022-06-07
2022-06-10
2022-06-11
2022-06-14
2022-06-17
2022-06-18
Can anyone help me with this logic?
What I tried was so dirty, I put a loop on range of dates, and got the list of all dates, and then i put another loop to get name of day of each date, and then compared each day name in an array of days & pushed that date to new array
Here is the code (Which works perfectly fine) but I need better solution:
function getDaysArray(start, end) {
for(var arr=[],dt=new Date(start); dt<=new Date(end); dt.setDate(dt.getDate()+1)){
arr.push(helperClass.getDateTime(new Date(dt)).date);
}
return arr;
}
function getDayName (dateStr, locale){
var date = new Date(dateStr);
return date.toLocaleDateString(locale, { weekday: 'long' });
}
var days = ["tuesday", "friday", "saturday"];
var getAllDates = getDaysArray("2022-06-02", "2022-06-20");
var getDates = [];
for(var i = 0; i < getAllDates.length; i++){
if(days.includes(getDayName(getAllDates[i]).toLowerCase())){
getDates.push(getAllDates[i])
}
}