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

dave_m13
Kilo Contributor

Brilliant James, thank you, owe you a beer.

Changed the code a little and it works a treat

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

var hours = dur.getNumericValue()/(60*60*1000); // numeric value is in milliseconds.
gs.addInfoMessage(hours);

var businessDays = Math.floor(hours / 9); // (rounding down) as long as there are always 9 hours in your "business day"
gs.addInfoMessage(businessDays);

var businessDuration = new GlideDuration(businessDays * (24*60*60*1000));
gs.addInfoMessage(businessDuration);

current.duration = businessDuration;

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()));

}


Not applicable

Ah yes, 24 hours in a day. I'll fix my example in case anyone else isn't quite so eagle-eyed.


kevinclark-7EL
Tera Contributor

I know this is an old post, but I just wanted to say thanks for putting this up all those years ago.   I used this as the basis of Duration Calculator where I wanted to know the rounded full day values based on a standard business day PLUS any "whole hours" that accompanied it.   Here's my code, may it help others (and potentially my future self):



var hoursCalc = duration/(60*60*1000);                                               // Divide the duration from milliseconds to hours - 60x60x1000


  var businessDays = Math.floor(hoursCalc / 24);                           // Find # of business days by rounding down the number of days into 24 hour blocks (ignoring additional hours for now)


  var hoursDiff = duration - businessDays*60*60*24*1000;           // Establish whether the rounding has removed any "whole hours" by calculating the difference between rounded business days and total duration.


  var durCalc = businessDays*(60*60*12*1000)+hoursDiff;             // Set the amount for the duration calculation based on business days in MS + the difference in hours.


  var durToAdd = new GlideDuration(durCalc);                                   // Translate into Duration Object