Duration Calculation based on Business Days/Hours

dave_m13
Kilo Contributor

Hi All

I have the following script that works as expected that sets the duration field based on the Start/End Dates with a schedule.

var dur = calcDurationSchedule(current.start_date,current.end_date);
current.duration = dur;

function calcDurationSchedule(start, end) {
// Get the user
var usr = new GlideRecord('sys_user');
usr.get(gs.getUserID());
// Create schedule - pass in the sys_id of your standard work day schedule and pass in the users timezone
var sched = new Packages.com.glide.schedules.Schedule('08fcd0830a0a0b2600079f56b1adb9ae',usr.time_zone);
return (sched.duration(start.getGlideObject(), end.getGlideObject()));
}

However I would like the duration field to return business days rather than 24 hours. So if Business day is 9 hours then this should be 1 day.

I have tried various things but cannot get anything to work. Has anyone else done anything similar?

7 REPLIES 7

Not applicable

Can you divide the duration value by the number of hours in the business day? Maybe give some example values and the result you want, if it's something more complicated than that.


dave_m13
Kilo Contributor

Thanks James

Where would I put this in the current script above.

Thanks


dave_m13
Kilo Contributor

Hi

Does anyone know how to divide the duration value in the above script.

At the moment as an example over 10 days the result with script is 54 hours, which shows as 2 days and 6 hours. However based on the schedule I want to see 6 days.

Thanks



Not applicable

Oh, I see the difficulty - you're getting a GlideDuration object (I guess you're following the example of http://wiki.service-now.com/index.php?title=Calculate_Duration_Given_a_Schedule) and you need to manipulate that.

Probably something like the following:



var dur = calcDurationSchedule(current.start_date,current.end_date);

var hours = dur.getNumericValue()/(60*60*1000); // numeric value is in milliseconds.
var businessDays = Math.floor(dur.getNumericValue() / 9); // (rounding down) as long as there are always 9 hours in your "business day"

var businessDuration = new GlideDuration(businessDays * (60*60*1000));