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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Chad,

Following links should be helpful:

https://community.servicenow.com/community?id=community_question&sys_id=11010fa5db98dbc01dcaf3231f96194f

Following example:

var date1 = new GlideDateTime(current.degradation_start + ' 00:00:00');

var date2 = new GlideDateTime(current.degradation_stop + ' 00:00:00');

var dateDiff = GlideDateTime.subtract(date1,date2);
var durationStr = dateDiff.getDurationValue();

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Chad7
Tera Expert

Even using your example, the field remains blank.

Abhinay Erra
Giga Sage

What are the field types of degradation_start, degradation_stop, degradation_duration

Respectively-- Date/Time, Date/Time, Duration