Get weekday list between two dates? JavaScript
Solution 1:
You can use a for
loop to loop through each date between the start and end date, then use Date.getDay
to get the day of the week and ignore the dates that are not a weekday.
function getWeekDayList(startDate, endDate) {
let days = []
let end = new Date(endDate)
for (let start = new Date(startDate); start <= end; start.setDate(start.getDate() + 1)) {
let day = start.getDay();
if (day != 6 && day != 0) {
days.push(new Date(start));
}
}
return days;
}
const result = getWeekDayList('2022-01-10', '2022-01-20')
console.log(result.map(e => e.toLocaleString('en-US', {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })))