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.

Date difference in business days

madanm7786
Mega Guru

Hi All,

I need to get the difference between 2 date fields in business days in Business Rule. Please help me on this.

For ex. i have 2 date fields, Date1 and Date2. If the difference between these 2 fields is 3 business days(Excluding Weekends) then i need to perform some action.

Intially i used gs.dateDiff function, but it will not give business days. Now i need to get business days.

Please help me on this.

Thanks,

Maddy

4 REPLIES 4

Raju Koyagura
Tera Guru

Try this 

var sgd1 = new GlideDate();
sgd1.setDisplayValue(current.u_date1); //change the element name
var sgd2 = new GlideDate();
sgd2.setDisplayValue(current.u_date2); //change the element name

duration= GlideDate.subtract(sgd1, sgd2);
gs.info(duration.getDisplayValue());

Hi Raju,

This will give the total number of days but not the business days.

Thanks,

Maddy

Trupti6
Tera Expert

Hi,

 

Below is the code to exclude weekends...might help you...

function getDateDiffExcWeekends(start , end){

 

//Make Sure that start and end are GlideDateTime Objects

 

  var days = 0 ;

 

  while (start < end) {

 

  start.addDays(1);

 

  if (start.getDayOfWeek() != 6 && start.getDayOfWeek() != 7)           //excluding Weekend

 

  {

 

  days++ ;

 

  }

 

  }

 

  return days;

 

}

 

Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.

 

Thanks,
Tripti S.

madanm7786
Mega Guru

Hi All,

Thanks for your support. I got the code in one of the community thread.

Below is the code,

var stdt = "2018-07-04 21:58:09"
var eddt = "2018-07-09 21:58:33";
var dt1 =new GlideDate();
dt1.setDisplayValue(stdt);
var dt2 =new GlideDate();
dt2.setDisplayValue(eddt);
var sched =new GlideSchedule('9c182780db13130071d61fc7689619b7');
var dur= (sched.duration(dt1,dt2).getDurationValue());
var f_dur = dur.split(' ')[0];
gs.print("Business days: "+dur);
gs.print("Final Business days: "+f_dur);

 

Thanks,

Maddy