Find next business day

Srinidhi Hari E
Kilo Contributor

Hi,

I used the below code to get my next business day. The issue here is I want to get Monday as the next business day if the currentDate is on Friday/ Sat / Sun.

For Friday it gives the result as Monday but for Sat and Sun it always gives me Tuesday as the next business day. I have been checking for so many workarounds, but I am not finding a solution.

The outputs are ( Nov 22 and 23 are holidays) ,

currentDate - 2018-11-21 (Wed) -> Next business day - 2018-11-26 (Mon)

currentDate - 2018-11-22 (Thurs) -> Next business day - 2018-11-27 (Tues)

currentDate - 2018-11-23 (Fri) -> Next business day - 2018-11-27 (Tues)

code,

checkDate();

function checkDate(){

    var currentDate = new GlideDateTime('2018-11-22 07:00:00');

    var days = 1;

    var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);

    var firstCal = dateCalc(currentDate, dur);

    //var firstnextDate = firstCal.getDate();

  //var firstnextTime = '08:00:00';

  //var first_u_hp_sla_start = firstnextDate+' '+firstnextTime;

  //finalDate = first_u_hp_sla_start;

  //gs.print('finalDate ' +finalDate);

}

function dateCalc(a,b){

var schedule = new GlideSchedule('223fc1b44f176e009bf687dab110c701'); //sys_id of 24*7 schedule

var end = schedule.add(a, b);

return end;

}

1 ACCEPTED SOLUTION

ginesdoleratorm
ServiceNow Employee
ServiceNow Employee

Hi Srinidhi,



To solve your issue, you can consider using the whenNext() method in the GlideSchedule. It gives you how much time (in millisecons) until we are in schedule time again.



Then, the logic would be:


1) Add one day to the current date.


2) Get how long until we are in the schedule again


3) Add that time to the current date.



The code would be something like this:


checkDate();


function checkDate(){


    var currentDate = new GlideDateTime('2018-11-22 07:00:00');


    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('b84159d7677132006e6eadab9485ef74'); //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;


}


View solution in original post

3 REPLIES 3

ginesdoleratorm
ServiceNow Employee
ServiceNow Employee

Hi Srinidhi,



To solve your issue, you can consider using the whenNext() method in the GlideSchedule. It gives you how much time (in millisecons) until we are in schedule time again.



Then, the logic would be:


1) Add one day to the current date.


2) Get how long until we are in the schedule again


3) Add that time to the current date.



The code would be something like this:


checkDate();


function checkDate(){


    var currentDate = new GlideDateTime('2018-11-22 07:00:00');


    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('b84159d7677132006e6eadab9485ef74'); //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;


}


Thank you Gines . The code was perfect !


Inactive_Us2002
Kilo Guru

Hi @ginesdoleratormo & @Srinidhi Hari Elangovan,

 

I need help for some more extend to it..I need to pass the business date to the below if condition, how can I achieve it?

(function executeRule(current, previous /*null when async*/) {

function checkBusinessday(date,day){
date.addDays(day);
var schedule = new GlideSchedule('d489cbdcdb18eb4090f992d5db961955'); //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
var date1 = date.toString().split(" ");
return date1[0];
}

if(current.comments.changes() && new GlideDateTime() > current.u_follow_up_start)
{
var ct = new GlideDateTime();
var fd = current.u_striked_on;
var diff =ct.compareTo(fd);
if(diff >= 2)
{
current.u_3_strike_counter += 1;
current.u_striked_on = new GlideDateTime();
}
}