getting number of working days between start date and end date

chidanandadhath
Kilo Guru

I want to get number of working days between start date and end date in a business rule.

1 ACCEPTED SOLUTION

add +1 to business_days. That should solve the issue

 

Use the below logic to add 1

var dc = new DurationCalculator();
		dc.setSchedule('eb65c2250fc76340e1f30dbce1050ee2', 'Los Angeles'); // Schedule sys_id and system time zone
		var dur = dc.calcScheduleDuration(start, end);
		
		var business_days = dur/(60*60*9); // Update the t
var gdt = new GlideDateTime(end);
var day_of_month = gdt.getDayOfWeekUTC();

if(day_of_month < 6)
business_days += 1;

View solution in original post

20 REPLIES 20

You can use something like this

duration.getNumericValue() / 1000 / 60 / 60 / 9;// 9 is the amount of work hours pr day in the schedule

var gdt1 = new GlideDateTime('2019-01-01');
var gdt2 = new GlideDateTime('2019-03-01');
 

var schedule = new GlideSchedule();
 
schedule.load('090eecae0a0a0b260077e1dfa71da828'); // loads "8-5 weekdays excluding holidays" schedule
var duration = schedule.duration(gdt1, gdt2);
 
gs.print(duration.getNumericValue() / 1000 / 60 / 60 / 9);

 

chidanandadhath
Kilo Guru

I'm trying to run in background script, tried below but not getting the output

var gr=new GlideRecord('requested_allocation');
gr.addQuery('resource_plan.resource_type','User');
gr.addQuery('resource_plan.user_resource.name','David Loo');
gr.addQuery('start_date','2019-03-01');
gr.query();
while(gr.next()) {

var gdt1 = new GlideDateTime(gr.start_date);
var gdt2 = new GlideDateTime(gr.end_date);

var schedule = new GlideSchedule();
schedule.load('08fcd0830a0a0b2600079f56b1adb9ae'); // loads "8-5 weekdays excluding holidays" schedule
var duration = schedule.duration(gdt1, gdt2);

}
gs.info(duration);
gs.info(duration.getNumericValue() / 1000 / 60 / 60 / 9);

Validate that you gdt1 and gdt2 are correct:

var gdt1 = new GlideDateTime(gr.start_date);
var gdt2 = new GlideDateTime(gr.end_date);

Might be

var gdt1 = new GlideDateTime(gr.getValue('start_date'));
var gdt2 = new GlideDateTime(gr.getValue('end_date'));

Because glidedatetime needs strings and not glide object

Nitesh Balusu
Giga Guru

This post is exactly what you need.

Calculate two business dates

dvp
Mega Sage
Mega Sage

Try the below script. 

Please update the field names, schedule sys_id, system time zone and total hours of a day in schedule

var start = new GlideDateTime(current.START_FIELD_NAME)
var end = new GlideDateTime(current.END_FIELD_NAME)

		
		var dc = new DurationCalculator();
		dc.setSchedule('eb65c2250fc76340e1f30dbce1050ee2', 'Los Angeles'); // Schedule sys_id and system time zone
		var dur = dc.calcScheduleDuration(start, end);
		
		var business_days = dur/(60*60*9); // Update the total hours of a day based on shedule
		

		//gs.log('start: ' + start + ' end:' + end )
		
		gs.log(business_days);
		
	}