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 10amvar 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
    addSchedule(dc);
 
    // 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
 */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');
    grSched.addQuery('name', scheduleName);
    grSched.query();if(!grSched.next()){
        gs.log('*** Could not find schedule "'+ scheduleName +'"');return;}
    durationCalculator.setSchedule(grSched.getUniqueValue(),"GMT");}