the calculate days function is returning wrong count

I have a javascript function and when i select dates like Nov 20 to Nov 22, i get a count of 2 instead of 3, what i am doing wrong here, i tried debugging the code but could not figure out what is going wrong

is it because of the timezone

enter image description here

Here is my JS Code

function dConvert(d, seprater="https://stackoverflow.com/") {
    let aFormate = dateFormat.split(seprater);
    let aD = d.split(seprater);
    let oD = aFormate.reduce((acc, val, ind) => {
        acc[val] = aD[ind];
        return acc;
    }, {});
    return oD['yy'] + '-' + oD['mm'] + '-' + oD['dd'];
}

Date.prototype.addDay = function(days) {
    var date = new Date(this.valueOf())
    date.setDate(date.getDate() + days);
    return date;
}


function calcBusinessDays(startDate, endDate) {
    var count = 0;
    var curDate = startDate;
    if (startDate == endDate) return 1;
    let aTemp1 = startDate.split("https://stackoverflow.com/");
    let aTemp2 = endDate.split("https://stackoverflow.com/");
    let dDate1 = new Date(dConvert(startDate));
    let dDate2 = new Date(dConvert(endDate));
    let aWD = $('[name="WorkingDays"]').val().split(',');
    while (dDate1 <= dDate2) {
        var dayOfWeek = (dDate1.getDay() + 1).toString();
        var isWworkingDay = aWD.includes(dayOfWeek);
        let oD = {
            'm': dDate1.getMonth() + 1,
            'd': dDate1.getDate(),
            'y': dDate1.getFullYear(),
        };
        let tempDate = (oD.m < 9 ? "0" + oD.m : oD.m) + "https://stackoverflow.com/" + (oD.d < 9 ? "0" + oD.d : oD.d) + "https://stackoverflow.com/" + oD.y;
        if (isWworkingDay && (aHolidayDate.indexOf(tempDate) == -1))
            count++;
        dDate1 = dDate1.addDay(1);
    }
    return count;
}

Please what is wrong here

Thanks

It seems that you’re wanting to include the first date in the calculation of your date range. If you’re wanting to do that you should have count = 1 instead of count = 0. If you don’t your while loop will be one short.

Leave a Comment