• London
• Kingston
• Jakarta
• Istanbul
• Helsinki
• Geneva
• Store
Close

## Simple duration vs relative duration

Log in to subscribe to topics and get notified when content changes.

# Simple duration vs relative duration

How much work is required to complete a task can be expressed as a "relative duration."

Relative duration determines the expected due date and time relative to the starting time. Examples of relative durations include "Next business day by 4pm," or "2 business days by 10:30am."

To calculate a relative duration, the calendar and time zone must be considered to determine what "next business day" means since it is the calendar that defines which days are valid work days and the time zone will affect the result as well. As an example, consider "Next business day by 4pm":
• If it is Monday at 12pm: Next business day by 4pm => Tuesday at 4pm
• If it is Friday at 2pm: Next business day by 4pm => the following Monday at 4pm
Note: Next business day is often defined by a starting day and time. For example, "next business day at 4pm if before 2pm" indicates that if the current time is after 2pm on a business day, then "Next business day" really means 2 business days since today does not count.

## Calculating a relative duration

An example of a relative duration calculation script.

This script calculates the relative duration for "Next day at 4pm if after 10am":
```// Next day at 4pm if before 10am
var days =1;
if(calculator.isAfter(calculator.startDateTime,"10:00:00"))
days++;

calculator.calcRelativeDueDate(calculator.startDateTime, days,"16:00:00");```

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

```/**
* Sample use of relative duration calculation.
*
*/

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

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

// First we need a DurationCalculator object. We will also use
// the out-of-box relative duration "2 bus days by 4pm"
var dc =new DurationCalculator();
var relDur ="3bf802c20a0a0b52008e2859cd8abcf2";// 2 bus days by 4pm if before 10am

// Since our start date is before 10:00am our result is two days from
// now at 4:00pm.
dc.setStartDateTime("5/1/2012 09:00:00");
if(!dc.calcRelativeDuration(relDur)){
gs.log("*** calcRelativeDuration failed");
return;}
gs.log("Two days later 4:00pm: "+ dc.getEndDateTime());

// Since our start date is after 10:00am our result is three days from
// now at 4:00pm.
dc.setStartDateTime("5/1/2012 11:00:00");
if(!dc.calcRelativeDuration(relDur)){
gs.log("*** calcRelativeDuration failed");
return;}
gs.log("Three days later 4:00pm: "+ dc.getEndDateTime());}

/**
* Add a specific schedule to the DurationCalculator object.
*
* @param durationCalculator An instance of 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');
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;}
durationCalculator.setSchedule(grSched.getUniqueValue(),"GMT");}```

## 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
*/