Calculate Business Day Difference between two dates?

snow_04
Giga Contributor

Hi,

How can we calculate business days between two dates i.e. one date value selected in Start Date Field and the other date would be the current Date(Today Date). I have tried the below code but it does not gives me the correct business days value:

gs.info(gs.now());
var date1 = '10/06/2020';
var dc = new DurationCalculator();
dc.setSchedule('Sys Id of the Schedule');
var dur = dc.calcScheduleDuration(date1,gs.now())/3600;
gs.info('Duration is' + dur);
1 ACCEPTED SOLUTION

Add date in correct format

var date1 = '2020-06-10';
var end = new GlideDateTime(date1);

 

And try again ...

 

 

Mark my ANSWER as CORRECT and HELPFUL if it helps...

 

Refrence:

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_GlideDateTimeAPI#r_G...

View solution in original post

7 REPLIES 7

Ct111
Tera Sage

Go through the below link thoroughly you will get your answer , check response of DVP

https://community.servicenow.com/community?id=community_question&sys_id=9f3900f1db8f2b407d3e02d5ca96...

 

Mark my ANSWER as CORRECT and HELPFUL if it helps

snow_04
Giga Contributor

Hi Creativethinker/Ankur

 

Have tried as per the links provided in the post, but the final duration gives me weird results always as 93 days. Below is my script:

 

var start = new GlideDateTime(gs.now());
gs.info('Start Date' + start);
var date1 = '10/06/2020';
var end = new GlideDateTime(date1);
gs.info('End Date' +end);
var dc = new DurationCalculator();
dc.setSchedule('Sys ID');
var dur = dc.calcScheduleDuration(start,end);
var business_days = dur/(60*60*8);
gs.info('Duration is' + business_days);
var gdt = new GlideDateTime(end);
var day_of_month = gdt.getDayOfWeekUTC();

if(day_of_month < 6)
business_days += 1;
gs.info('Duration is' + business_days);

 

Please correct me where I have doing wrong. Motive is to get the difference from the date selected by the user against the Today Date.

 

Result:

*** Script: Start Date2020-05-27 00:00:00
*** Script: End Date2020-10-06 00:00:00
*** Script: Duration is93.125
*** Script: Duration is94.125

Add date in correct format

var date1 = '2020-06-10';
var end = new GlideDateTime(date1);

 

And try again ...

 

 

Mark my ANSWER as CORRECT and HELPFUL if it helps...

 

Refrence:

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_GlideDateTimeAPI#r_G...

Hi,

I believe the output is correct

If you see the total difference in days between 28th May and 6th October -> 131 days

Now from 27th may to 6th October how many weekend days are there -> 37

So 131-37 = 94

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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