add Duration to Date/Time

jeremyphillips
Kilo Contributor

Howdy--

I have a record that has the following fields:

u_start_time (Date/Time)

u_end_time (Date/Time)

u_activity_duration (Duration)

In a Business Rule, I am trying to add u_activity_duration to u_start_time to calculate u_end_time.

I have tried the following:

(function executeRule(current, previous /*null when async*/) {

      // calculates the end time of an activity based on the start time and duration

  gs.log("Start time: " + current.u_start_time);

  gs.log("Duration: " + current.u_activity_duration);

  gs.log("Calculated end time: " + current.u_start_time.add(current.u_activity_duration));

  current.u_end_time = current.u_start_time.add(current.u_activity_duration);

})(current, previous);

That results in the following logs:

Start time: 2016-07-11 15:23:48

Duration: 1970-01-01 00:59:00

Calculated end time: undefined

After searching these forums, I have tried many variations on the script above, including using getValue, getNumericValue, etc., all with the same results.

Any suggestions on how I can add the Start Time and Duration to calculate and End Time?

Thanks,

Jeremy.

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here you go



var gdt = new GlideDateTime(current.u_start_date.getDisplayValue());


var ms=current.u_activity_duration.dateNumericValue();


gdt.add(ms);


current.end_date=gdt.getValue();


View solution in original post

11 REPLIES 11

Thanks Abhinay and Chuck!



Between both of your responses, I got it working.



Here's the working script I ended up with:



(function executeRule(current, previous /*null when async*/) {


      // calculates the end time of an activity based on the start time and duration


  var startTime = new GlideDateTime(current.u_start_time.getDisplayValue());


  var duration = current.u_activity_duration.dateNumericValue();


  startTime.add(duration);


  gs.log("Calculated end time: " + startTime);


  current.u_end_time = startTime.getDisplayValue();


})(current, previous);



It seems odd to me that you have to convert to "human readable" using getDisplayValue() rather than just working with the GlideDateTime and GlideDuration objects directly, but it's working now and I'm happy!



Best, and thanks again,



Jeremy.


Thanks for posting your final solution and   marking the appropriate response complete.


At the end it should be either


current.u_end_time = startTime.getValue();


or


current.setDisplayValue('u_end_date',startTime.getDisplayValue());


Thanks, Abhinay!



It was working with getDisplayValue, but I changed to getValue and confirmed that it works that way, too. I'm guessing that getValue is "safe" across timezones and user settings while getDisplayValue might break?



J.


Hi Jeremy,



In short -


getValue() retrieves the UTC time value


getDisplayValue() retrieves the system timezone value.