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

Nice catch however when I did as you said its working fine but unfortunately, I don't have access to create a schedule and to add it to the holidays schedule as it involves typical process in my side. as it is a high priority ask, I need to do this without it.

can you suggest me where I need to modifications if possible modified code.

@KM SN 

without using schedule how and where would you store the holidays and the work timings?

I will suggest to use Schedule to make your life easier.

are you planning to use system property to hold holiday dates?

If yes then simply add 1 day to current date/time and see if the date which came is holiday by checking in system property, also check if it's Sat/Sun (Weekend)

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

I have schedule which consist only holidays it don't consider weekends I am saying.

Hello @KM SN ,

 

Please try the following script:

var scheduleId = '6874e8bb8320221019aff496feaad333',
    currentDate = new GlideDateTime(),
    nextWorkingDay = getNextWorkingDay(currentDate, scheduleId);

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

function getNextWorkingDay(date, scheduleId) {
    var schedule = new GlideSchedule(scheduleId),
        loopCount = 0;
    if (!schedule.isValid()) return;
    date.addDays(1);
    while (!schedule.isInSchedule(date) || isWeekEnd(date)) {
        date.addDays(1);
        if (++loopCount > 100) return;
    }
    return date;
}

function isWeekEnd(date) {
    return date.getDayOfWeekLocalTime() > 5;
}

 

When running against a 24x7 Schedule where April 4 and 7th are excluded as holidays it returns:

next working day is 2025-04-08 09:11:37

 

It also prevents infinite loops in case of weird schedule configuration.

 

Regards,

Robert

 

 

 

I am not able to run it in background script, can you tune this to background script to check the output. When I modified above code as below we I am getting the value as undefined.

var scheduleId = '9b8af1c083b822107abbb7d6feaad343',
	currentDate = new GlideDateTime(),
	nextWorkingDay = getNextWorkingDay(currentDate, scheduleId);
	
gs.print('next working day is ' + nextWorkingDay);

function getNextWorkingDay(date, scheduleId) {
    var schedule = new GlideSchedule(scheduleId);
	loopCount = 0;
	if (!schedule.isValid()) return;
	date.addDays(1);
	while (!schedule.isInSchedule(date) || isWeekEnd(date)) {
		date.addDays(1);
		if (++loopCount > 100) return;
	}
	return date;
}

function isWeekEnd(date){
 if(date.getDayOfWeekLocalTime() > 5){
 return true;
 }
}