Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hello @KM SN ,

 

I'm sorry, I wrote the script using some JavaScript features that are only supported in the latest releases. I have updated my previous post with a script that should work fine on all releases.

 

Regards,

Robert

//var scheduleId = '9b8af1c083b822107abbb7d6feaad343';
var scheduleId = '090eecae0a0a0b260077e1dfa71da828';		// 8-5 weekdays excluding holidays
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;
	}
	else
		return false;
} 

There is the OOB "8-5 weekdays excluding holidays" schedule in my PDI. If you want to just use weekdays. Open the OOB "8-5 weekdays excluding holidays", right-click on the page header and Select "Insert and Stay" to create "Weekdays excluding holidays" and add a Schedule entry "Monday - Friday", and a Child Schedule: "U.S. Holidays" or create one for the Holidays you want to use.

 

Screenshot 2025-04-04 143349.pngScreenshot 2025-04-04 143413.pngScreenshot 2025-04-04 143436.png