# Calculating a simple duration

This business rule and script example demonstrate how to calculate a simple duration.

var dur =new DurationCalculator();
dur.setSchedule(current.schedule);
dur.setStartDateTime("");

if(current.duration_type==""){
dur.calcDuration(current.duration.getGlideObject().getNumericValue()/1000);}else{
dur.calcRelativeDuration(current.duration_type);}

current.end_date_time= dur.getEndDateTime();
current.work_seconds= dur.getSeconds();

This script demonstrates how to use DurationCalculator to calculate a simple duration.

/**
* Sample script demonstrating use of DurationCalculator to compute simple durations
*
*/

gs.include('DurationCalculator');
executeSample();

/**
* Function to house the sample script.
*/function executeSample(){

// First we need a DurationCalculator object.var dc =new DurationCalculator();

// Compute a simple duration without any schedule. The arguments// can also be of type GlideDateTime, such as fields from a GlideRecord.var dur = dc.calcScheduleDuration("5/1/2012","5/2/2012");
gs.log("calcScheduleDuration no schedule: "+ dur);// 86400 seconds (24 hours)

// The above sample is useful in limited cases. We almost always want to // use some schedule in a duration computation, let's load a schedule.

// Compute a duration using the schedule. The schedule// specifies a nine hour work day. The output of this is 32400 seconds, or// a nine hour span.
dur = dc.calcScheduleDuration("5/23/2012 12:00","5/24/2012 12:00");
gs.log("calcScheduleDuration with schedule: "+ dur);// 32400 seconds (9 hours)

// Compute a duration that spans a weekend and holiday. Even though this// spans three days, it only spans 9 work hours based on the schedule.
dur = dc.calcScheduleDuration("5/25/2012 12:00","5/29/2012 12:00");
gs.log("calcScheduleDuration with schedule spaning holiday: "+ dur);// 32400 seconds (9 hours)

// Use the current date time in a calculation. The output of this is// dependent on when you run it.var now =new Date();
dur = dc.calcScheduleDuration("5/15/2012",new GlideDateTime());
gs.log("calcScheduleDuration with schedule to now: "+ dur);// Different on every run.}

/**
* Add a specific schedule to the DurationCalculator object.
*
* @param durationCalculator An instance of DurationCalculator
*/function addSchedule(durationCalculator){//  Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.var scheduleName ="8-5 weekdays excluding holidays";var grSched =new GlideRecord('cmn_schedule');