How to check next working day excluding holiday and week ends ?

KM SN
Tera Expert

a variable contains how many days its need to add after adding the days to current date I need to check its holiday or not from schedule and also I need to check through script whether it's weekend or not.

At end I need to get the next working day which should neither holiday nor weekend.

checkDate();



function checkDate(){

    var currentDate = new GlideDateTime();

    var nextWorkingDay = getNextWorkingDay(currentDate);

    gs.print('next working day is ' + nextWorkingDay);

}

function getNextWorkingDay(date){

    date.addDays(1);     // step 1. We add one day to the current date

    var schedule = new GlideSchedule('6874e8bb8320221019aff496feaad333'); //sys_id of 24*7 schedule

    var whenNext = schedule.whenNext(date);     // step 2. how long until we are in schedule again

    date.add(whenNext);     // step 3. add the time until we are in schedule to the current date

    return date;

}

 

11 REPLIES 11

Medi C
Giga Sage

Hello @KM SN,

 

Could you please try:

function checkDate() {
    var daysToAdd = 1; // Change this to your actual variable
    var currentDate = new GlideDateTime();
    currentDate.addDays(daysToAdd);

    var nextWorkingDay = getNextWorkingDay(currentDate);
    gs.print('Next working day is: ' + nextWorkingDay);
}

function getNextWorkingDay(date) {
    var schedule = new GlideSchedule('6874e8bb8320221019aff496feaad333'); // Replace with your actual schedule

    while (true) {
        // Check if date is a valid working time in the schedule
        if (schedule.isInSchedule(date)) {
            var jsDate = new Date(date.getValue());
            var day = jsDate.getDay(); // 0 = Sunday, 6 = Saturday

            if (day !== 0 && day !== 6) {
                return date; // Not a weekend, and inside schedule — we're good
            }
        }

        // Not valid — add one day and try again
        date.addDays(1);
    }
}

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Neither its showing error nor anything.

I just declared 4th and 7th as holidays in schedule so typically it should return me 8th of this month. right?

Ankur Bawiskar
Tera Patron
Tera Patron

@KM SN 

you can refer this link on adding business day using scheduled, you can pass 1 day and it will give you the next working day

Add Business Days 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Robert H
Mega Sage

Hello @KM SN ,

 

Is there a reason why you are using the sys_id of a 24 * 7 schedule?

Why not create a 24 * 5 (Monday to Friday, all day) Schedule and add the holidays, then your existing script would return the expected results because the Schedule would exclude the weekends automatically.

 

Please note that you should only call schedule.whenNext() when you have first checked if schedule.isInSchedule() returns false, else there will be unexpected results. See documentation.

 

Regards,

Robert