How to find business days between 2 dates from form through client script ?

Snehal13
Kilo Sage

How to find business days between 2 dates from form through client script ? 

 

It must exclude the weekends. Note - This needs to be achieved from client script when start date and end dates are filled and immediately the duration field below it must populate the days

7 REPLIES 7

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Snehal13 

 

For that you need to first have a Schedule in [cmn_schedule] table which will exclude weekends and if any other holidays and include only business days. (You will get many OOTB Schedules in the table which you can refer and reuse or can create your own).

 

Now you can use GlideSchedule() API to calculate duration which is Server Side API so you have to use Script Include.

 

You can pass the values of your 2 dates as parameters for this Script Include Function. Than can use below code to calculate duration:

 

var startDate = new GlideDateTime(start_Date); // 1st Date parameter
var endDate = new GlideDateTime(end_date); //2nd Date parameter
var schedule = new GlideSchedule();
 
schedule.load('090eecae0a0a0b260077e1dfa71da828'); // sys_id of your Schedule you created for Business Days
var duration = schedule.duration(startDate, endDate);
return duration.getDurationValue(); // gets the elapsed time in schedule

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Is there away to calculate days between 2 dates in a client script ? I would like to avoid using a server side script.

Hi @Snehal13 

 

This solution is for Client Script only using Script Include. You have to right this code in Script Include and call it from client script.

I don't think we can check through client side scripting.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

exclude weekend ?