I'm trying to figure out if there is a way to figure how many weekend days exist between two date ranges.
Here is currently what I have in JS
function isWeekend(date1, date2) {
var d1 = new Date(date1),
d2 = new Date(date2),
weekendDays = 0;
while (d1 < d2) {
var day = d1.getDay();
if(day == 6 || day == 0){
weekendDays++;
}
d1.setDate(d1.getDate() + 1);
}
return weekendDays;
}
Currently for some reason regardless of what date range it is, it seems to keep returning 1
What I want to basically achieve is that if the date range have Sunday it returns 1 and if it has Saturday AND Sunday it returns 2 and if it just has Saturday it returns 1.
Can someone lend me a helping hand to figure this out?
Thank you for taking your time to read this.