Calculate business days duration between 2 date fields

Priyanka145
Tera Contributor

Hi,

How to Calculate business days duration between 2 date fields

1 ACCEPTED SOLUTION

Hi,

907200 is seconds and it comes to 10.5 days.

Please verify it from your side what does that schedule contains, holidays etc

8 to 5 means 9 hours -> and it doesn't mean 1 day

1 day -> 24 hours

Also check the docs

Using DurationCalculator to calculate a due date

DurationCalculator calcScheduleDuration Mystery

If you take the start date as 15th June, 2022 and End Date as 23rd July, 2022

1) it comes to 28 working days excluding weekends; I am not sure if there are holidays in your schedule, I checked 8 -5 excluding holidays in my instance and there is no US holiday in it

2) so it comes to 28(days)*9(hours per day) = 252 hours

3) and 9hrs = 1 business day

4) so it comes to 252/9 = 10.5 business days

Regards
Ankur

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

View solution in original post

19 REPLIES 19

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

if you just want to exclude weekends then use this

var start = new GlideDateTime('<start>');
var end = new GlideDateTime('<end>');

var days = getDateDiffExcWeekends(start, end);
gs.info(days);

function getDateDiffExcWeekends(start , end){
	var days = 0;
	while (start < end) {
		start.addDaysUTC(1);

		if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7){
			days++ ;
		}
	}
	return days;
}

Regards
Ankur

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

Thanks Ankur, could you also kindly let me know how I can exclude holidays

Hi Ankur,

Thanks for the script, it worked exactly well and gave me the number of days difference excluding saturday and sunday.

But I also want to exclude holidays as well, where in having a schedule.

Hi Ankur, please help me with the script to exclude holidays as well