BobbyJones
BobbyJones

Reputation: 1354

Adding additional days but only on a Weekday (Mon-Fri) and skipping the weekend

How can the function below be modified such that if getdate('add',2) was called and that if the present date was to land on a Friday ie. (Feb. 23, 2015) the additional 2 "work week" dates would bring the new "work week" date to Feb. 27, 2015.

I need the function to basically skip the weekend. If the present date lands on Friday.

function getdate(type,y) {
    if (type == 'add') {
        var someDate = new Date();
        var numberOfDaysToAdd = y
        someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 
        var dd = someDate.getDate();
        var mm = someDate.getMonth() + 1;
        var yyyy = someDate.getFullYear();
        return dd + '/'+ mm + '/'+ yyyy
    }
}

Upvotes: 0

Views: 147

Answers (1)

Elec
Elec

Reputation: 1744

I use while to check if every next day is weekend day or not,if yes then skip it.

function getdate(type,y) {
    if (type == 'add') {
        var someDate = new Date();
        var numberOfDaysToAdd = y;
        var i=1;
        while(i<=numberOfDaysToAdd){
            someDate.setDate(someDate.getDate() + 1); 
            if(someDate.getDay() != 0 && someDate.getDay() != 6){//if the day isn't weekend days,then count it.
                i++;
            }

        }
        var dd = someDate.getDate();
        var mm = someDate.getMonth() + 1;
        var yyyy = someDate.getFullYear();
        return dd + '/'+ mm + '/'+ yyyy
    }
}

so this code you can skip all weekend days even if the added date is next 2 or more weeks.

Upvotes: 1

Related Questions