Calculate Business days from two dates field

Aayushi tandon
Tera Contributor

Hii Team,

There is two dates fields Start date and end Date, my requirement is :

I need difference between of these two dates , but only need to calculate business day 

For Example : if i select start date 2 may 2022 and end date 15 may 2022, there date difference is 14 days, but i need answer 10 days as 4 days are weekend.

I wrote these script: 

var dur = gs.dateDiff(current.start_date, current.end_date);
    var dateDifference = new GlideDuration(dur);
    var d = dateDifference.getDayPart();

gs.addInfoMessage(d);

By these script , my total 14 days is coming, but i am not able to understand , how i calculate only weekdays.

Need Help

Thanks in advance!

1 ACCEPTED SOLUTION

Neeraj31
Mega Sage

Hi Aayushi,

You need to create one schedule in which you need to exclude the weekends and set the time of business days and use sys ID of schedule in below code.

var dc = new DurationCalculator();
dc.setSchedule('<sys ID of schedule>'); 
var diff = dc.calcScheduleDuration(current.start_date, current.end_date) ;

 

Please mark reply as Correct/Helpful, if applicable. Thanks!

Thanks and Regards,

Neeraj Sharma

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hello,

When you say business day, if you want to exclude holidays as well.. then using schedule is a better option.

 Please find below ref - 

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

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Community Alums
Not applicable

Hello @Aayushi tandon  

Did you get chance to look into solution provided by me. Please let me know if you need any help.

If my answer helped you in any way, please then mark it as Correct/helpful.

Neeraj31
Mega Sage

Hi Aayushi,

You need to create one schedule in which you need to exclude the weekends and set the time of business days and use sys ID of schedule in below code.

var dc = new DurationCalculator();
dc.setSchedule('<sys ID of schedule>'); 
var diff = dc.calcScheduleDuration(current.start_date, current.end_date) ;

 

Please mark reply as Correct/Helpful, if applicable. Thanks!

Thanks and Regards,

Neeraj Sharma

Hi Aayushi,

Please mark reply as Correct/Helpful, if applicable. Thanks!

Thanks and Regards,

Neeraj Sharma