Understanding and changing the OOB Calculation for Business duration

EricG2
Tera Guru

Hey Team:

 

I'm having a lot of difficulty finding how the OOB methodology for Business Duration is calculated.

I've seen several posts with scripts available, and they all return bad information or NAN values when i attempt them.

 

My script is pretty simple for the moment

    var answer = '';
    var start = new GlideDateTime(current.work_start);
    var end   = new GlideDateTime(current.work_end);
	var now = new GlideDateTime();
	
	answer = GlideDateTime.subtract(end,start);

	gs.info("The new duration is  "+answer);

 

My goal is to compare the two date/time fields and return the # days and # hours between the two.

currently I'm getting this as my answer "The new duration is 1969-12-01 07:38:53"

Start date = 04/07/2026 15:43:23

End date = 05/08/2026 08:05:44

 

The OOB method returns a value of 7 Days 11 Hours 16 Minutes.  Which is the format i want.

However, if I understand the methodology, the duration calcs this using 24 hours.  Which is probably correct, however, I want it to be 10 hours (time during the business day) so that duration is more like 24 days(which is what i get in Excel).  If i do a straight difference between Start and End dates, I do get 31 days.

 

1 REPLY 1

k_lutz
Giga Sage

Hi @EricG2 

 

Date calculation is always so fun in SN! It sounds like you would want to use a schedule that reflects the 10 hour business day. I found one in ours and used that sys_id. It may be different in yours and this was just one way to put it together. I am sure there are other ways depending on your ultimate design. Hope this helps:

 

function getBusinessDurationDisplay(startField, endField, scheduleSysId, hoursPerBusinessDay) {

if (!startField || !endField) {
return '';
}

var start = new GlideDateTime(startField);
var end = new GlideDateTime(endField);

var sched = new GlideSchedule();
sched.load(scheduleSysId);

var duration = sched.duration(start, end);
var ms = duration.getNumericValue();

var msPerMinute = 1000 * 60;
var msPerHour = msPerMinute * 60;
var msPerBusinessDay = msPerHour * hoursPerBusinessDay;

var days = Math.floor(ms / msPerBusinessDay);
ms = ms % msPerBusinessDay;

var hours = Math.floor(ms / msPerHour);
ms = ms % msPerHour;

var minutes = Math.floor(ms / msPerMinute);

return days + ' Days ' + hours + ' Hours ' + minutes + ' Minutes';
}


// Test values from your example
var answer = getBusinessDurationDisplay(
'2026-04-07 15:43:23',
'2026-05-08 08:05:44',
'445cefd897007d10beddb4221153af8a',
10
);

gs.info('Test business duration: ' + answer);