Getting the remaining days of the current week I'm trying to get the remaining days of the current week, excluding weekends and past days, and I'm lazily checking for possible continuity of the week in the next month. This is not the best solution that I can think of (and even so, perhaps there are flaws), is there a simpler and clean way to achieve what I want?

function getDaysInMonth(month, year, holidays) {
    var date = new Date(year, month, 1);
    var today = new Date();
    today.setHours(0, 0, 0, 0);
    var days = [];
    var firstDay = 0;
    var remaining = [];
    var isWeekend = (today.getDay() === 6) || (today.getDay() === 0);
    while (date.getMonth() === month) {
        if ((date &gt;= today) &amp;&amp; (date.getDay() != 0) &amp;&amp; (date.getDay() != 6)) {
            days.push(new Date(date));
        }
        date.setDate(date.getDate() + 1);
    }
    month += 1;
    while (date.getMonth() === month) {
        if ((date.getDay() != 0) &amp;&amp; (date.getDay() != 6)) {
            days.push(new Date(date));
        }
        date.setDate(date.getDate() + 1);
    }
    if (holidays) {
        for (var index = 0; index &lt; holidays.length; index++) {
            var holiday = new Date(holidays[index]);
            for (var id = 0; id &lt; days.length; id++) {
                if (holiday.getTime() === days[id].getTime()) {
                    days.splice(id, 1);
                    break;
                }
            }
        }
    }
    if (!isWeekend) firstDay = days[0].getDay();
    for (var index = 0; index &lt; days.length; index++) {
        if (isWeekend) {
            remaining.push(new Date(days[index]));
            if (index === 4) break;
        } else {
            if (days[index].getDay() &gt;= firstDay) {
                remaining.push(new Date(days[index]));
            } else {
                break;
            }
        }
    }
    return remaining;
}


Update: I've expanded the code to query for possible holidays, holidays an array of dates (mm/dd/YYY).
First of all, never calculate dates yourself. If possible, always defer to a library that's built by people who know more about dates, who keeps rules up to date, and has more eyes on it to spot bugs than you and your code. For that, I forward you to moment.js. With that, calculating the remaining week days would be as simple as finding today's day number with day and subtracting it from 6 (Saturday's number).

const daysUntilWeekend = 6 - moment().day()


For work days (week days sans holidays) you simply need at least 3 moment instances, the first representing the start of the week, the second representing the end of the week, and the third (or more) representing the holidays. Then you can use isBetween to check if any of your holiday dates fall between the first two, and subtract how many match from 6 accordingly.

// May fail when today's saturday or sunday. Adjust this code accordingly.
const start = moment().day(1).startOf('day')
const end = moment().day(6).startOf('day')
const holidayMoments = [moment(), moment()] // Holiday dates in moments
const holidays = holidayMoments.filter(h =&gt; h.isBetween(start, end)).length
const today = moment().day()
const remainingWorkDays = 6 - (today + holidays)