Create an array of working dates between start date and end date in JavaScript

JavaScript Code

Share

In this tutorial we will create an array of Working Dates between a Start Date and an End Date also considering official holidays and working weekends.

Create a date array between start date and end date

This is similar to the Create an array of Dates between start date and end date in JavaScript tutorial.

var getDateArray = function(start, end) {
    var arr = new Array();
    var dt = new Date(start);
    while (dt <= end) {
        arr.push((new Date(dt)).toString().substring(0,15)); //save only the Day MMM DD YYYY part
        dt.setDate(dt.getDate() + 1);
    }
    return arr;
}

Function to prepare date array

Assuming we have an array officalHolidays containing holiday dates and another array workingWeekends containing working weekend dates. We will be preparing date array using them.

var prepareDateArray = function(dtArr) {
    var arr = new Array();
    for (var i = 0; i < dtArr.length; i++) {
        arr.push((new Date(dtArr[i])).toString().substring(0,15)); //save only the Day MMM DD YYYY part
    }
    return arr;
}

Function to find working dates

Once we have the array containing all the dates between the start date and an end date. And an array containing all the holiday dates and another array containing all the working weekend dates, we can move on and find the working dates using the following function.

In the given code it is assumend that Saturday and Sunday are holidays.

var getWorkingDateArray = function(dates, hoildayDates, workingWeekendDates) {
    
    // remove holidays
    var arr = dates.filter(function(dt){
        return holidaysArray.indexOf(dt) < 0;
    });

    // remove weekend dates that are not working dates
    var result = arr.filter(function(dt){
        if (dt.indexOf("Sat") > -1 || dt.indexOf("Sun") > -1) {
            if (workingWeekendDates.indexOf(dt) > -1) {
                return dt;
            }
        }
        else {
            return dt;
        }
    });
    
    return result;

}

Complete Code

/**
 * this will return an array containing all the
 * dates between start date and an end date
 */
var getDateArray = function(start, end) {
    var arr = new Array();
    var dt = new Date(start);
    while (dt <= end) {
        arr.push((new Date(dt)).toString().substring(0,15)); //save only the Day MMM DD YYYY part
        dt.setDate(dt.getDate() + 1);
    }
    return arr;
}

/**
 * this will prepare a date array
 */
var prepareDateArray = function(dtArr) {
    var arr = new Array();
    for (var i = 0; i < dtArr.length; i++) {
        arr.push((new Date(dtArr[i])).toString().substring(0,15)); //save only the Day MMM DD YYYY part
    }
    return arr;
}

/**
 * this will return an array consisting of the
 * working dates
 */
var getWorkingDateArray = function(dates, hoildayDates, workingWeekendDates) {
    
    // remove holidays
    var arr = dates.filter(function(dt){
        return holidaysArray.indexOf(dt) < 0;
    });

    // remove weekend dates that are not working dates
    var result = arr.filter(function(dt){
        if (dt.indexOf("Sat") > -1 || dt.indexOf("Sun") > -1) {
            if (workingWeekendDates.indexOf(dt) > -1) {
                return dt;
            }
        }
        else {
            return dt;
        }
    });
    
    return result;

}

// start and end dates
var startDate = new Date("2017-10-01"); //YYYY-MM-DD
var endDate = new Date("2017-10-14"); //YYYY-MM-DD

/**
 * holidays and working weekends
 *
 * if not applicable then set it as an empty array
 * example: if no offical holidays then set
 * officalHolidays = []
 * similarly, if no working weekends then set
 * workingWeekends = []
 */
var officalHolidays = ["2017-10-02"]; //YYYY-MM-DD
var workingWeekends = ["2017-10-07"]; //YYYY-MM-DD

// compute date array between start and end dates
var dateArray = getDateArray(startDate, endDate);

// prepare the holidays array
var holidaysArray = prepareDateArray(officalHolidays);

// prepare the working weekends array
var workingWeekendsArray = prepareDateArray(workingWeekends);

// get the working days array
var workingDateArray = getWorkingDateArray(dateArray, holidaysArray, workingWeekendsArray);

// output
console.log(workingDateArray);

Output

Tue Oct 03 2017

Wed Oct 04 2017

Thu Oct 05 2017

Fri Oct 06 2017

Sat Oct 07 2017

Mon Oct 09 2017

Tue Oct 10 2017

Wed Oct 11 2017

Thu Oct 12 2017

Fri Oct 13 2017