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.

Count number of days excluding weekends, Holidays.

Miguel1
Tera Contributor

Hi Experts!
I made business rule that count the number of days between two dates and it works but I would like to know how to exclude weekends 

for example if te user put 24-04-2020 in 'comienzo' field and 28-04-2020 in 'Fin' field the script returns 4 days but there is a weekend between these dates, so, How can I exclude that weekend? and the script returns 2 days. Same example for holidays like July 5th.

If anyone knows how I could do this I would really appreciate it!

thanks so much!!

 

(function executeRule(current, previous /*null when async*/) {
var realCom = new GlideDate(); 
realCom.setDisplayValue(current.u_comienzo_real.getDisplayValue()); //Field comienzo

var realFin = new GlideDate();
realFin.setDisplayValue(current.u_fin_real.getDisplayValue()); //Field Fin 

var seconds = gs.dateDiff(realCom,realFin,true);
var value = Math.floor(seconds/86400);
var x = parseInt(value,10);
current.setValue("u_duracion_real_dias",x + " DĂ­as");


})(current, previous);

find_real_file.png

 

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Check this:

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

Basically you need to use schedule to calculate this date.

Thanks,
Ashutosh

View solution in original post

5 REPLIES 5

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Check this:

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

Basically you need to use schedule to calculate this date.

Thanks,
Ashutosh

HI,

Any update on this.


Thanks,
Ashutosh

Hi Ashutosh!

I based on the post and it worked, thank you very much

rajneeshbaranwa
Tera Guru

You need to create a schedule excluding holidays and then use GlideSchedule.duration function to calculate difference between two datetime field.

Something like below.

 

var schedule = new GlideSchedule('sys_id_of_your_schedule');

schedule.duration(gdt1,gdt2);

 

Note that the returned duration value will be in second.

 

Please mark correct/helpful, if my answer helped you.