Calcualting duration between two date fields in scoped application

Chad7
Tera Expert

I'm used to calculating duration fields using dateDiff, but since that doesn't work in my scoped application, I'm having to use GlideDateTime.subtract, which I can't get to work on my business rule that is triggered when the start date or stop date changes. Can anyone tell me why this script isn't working as written? I get no errors, but the degradation_duration field just remains blank.

var start = new GlideDateTime(current.degradation_start.getDisplayValue());
var stop = new GlideDateTime(current.degradation_stop.getDisplayValue());
var duration = GlideDateTime.subtract(start, stop).getDisplayValue();

current.degradation_duration = duration.getDisplayValue();
1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

You are almost there, only think is you need to take out the display value while setting to the duration. Use this code

 

var start = new GlideDateTime(current.degradation_start.getDisplayValue()); 
var stop = new GlideDateTime(current.degradation_stop.getDisplayValue()); 
var duration = GlideDateTime.subtract(start, stop); 
current.degradation_duration = duration;

 

View solution in original post

6 REPLIES 6

Abhinay Erra
Giga Sage

You are almost there, only think is you need to take out the display value while setting to the duration. Use this code

 

var start = new GlideDateTime(current.degradation_start.getDisplayValue()); 
var stop = new GlideDateTime(current.degradation_stop.getDisplayValue()); 
var duration = GlideDateTime.subtract(start, stop); 
current.degradation_duration = duration;

 

Thanks, that seems to have done the trick.