The CreatorCon Call for Content is officially open! Get started here.

How to get business Days between two dates?

Sergio Gonzalez
Tera Contributor

Hi, I'm trying to get business days with a PA script using created field 

I got this:

var start = new GlideDateTime(current.sys_created_on);
var end = new GlideDateTime(start);

function getDateDiffExcWeekends(start , end){
var days = 0 ;
while (start < end) {
start.addDays(1);
if (start.getDayOfWeek() != 6 && start.getDayOfWeek() != 7) //excluding Weekend
{
days++ ;
}
}
return days;
}

getDateDiffExcWeekends(start , end);

 

but, I have 0 in the Indicator Records

2 REPLIES 2

Sukraj Raikhraj
Kilo Sage

Hope this link helps:

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

Community Alums
Not applicable

You need to add reference of schedule in your script so that script can take holidays and weekends into consideration. Something like below.

 

 

var startDateTime = new GlideDateTime("2020-11-02 11:00:00");

var endDateTime = new GlideDateTime();

var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set

dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');

dur.calcScheduleDuration(startDateTime, endDateTime);

var secs = dur.getSeconds();

var totalSecs = dur.getTotalSeconds(); gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);