GlideDateTime.subtract return negative value in days for Duration Field Type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2020 09:00 AM
Hi Guys,
Need your assistance as to why the result of my GlideDateTime.subtract returns negative values on days for the duration field type that I created. I have created a field name "Duration" which is a duration field type and I have 2 Date/Time Fields
Target Start Date and Target End Date, this are scoped application so I use the GlideDateTime.subtract to get the difference of 2 fields unfortunately it return negative result on days for the duration field.
Please see my script below:
setDurationFields();
function setDurationFields() {
var opened = new GlideDateTime(current.u_target_start_date.getDisplayValue());
var closed = new GlideDateTime(current.u_target_end_date.getDisplayValue());
if (current.u_duration.nil())
var datediffer = GlideDateTime.subtract(opened,closed);
current.u_duration = datediffer;
}
and this is the screenshot of the result:
Thanks in Advance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2020 09:05 AM
try now
setDurationFields();
function setDurationFields() {
var opened = new GlideDateTime(current.u_target_start_date);
var closed = new GlideDateTime(current.u_target_end_date);
if (current.u_duration.nil())
var datediffer = GlideDateTime.subtract(opened,closed);
current.u_duration = datediffer;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2020 09:44 AM
can you also add log. if its not working .
validate the log.
setDurationFields();
function setDurationFields() {
var opened = new GlideDateTime(current.u_target_start_date);
var closed = new GlideDateTime(current.u_target_end_date);
gs.info('Dates are '+ opened + ' '+ closed );
if (current.u_duration.nil())
var datediffer = GlideDateTime.subtract(opened,closed);
gs.info('duration is '+ datediffer.getNumericValue() );
current.u_duration = datediffer;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2020 10:58 AM
Hi Harshvardhan,
Thank you for showing interest in my case. I did what you have suggested and still the same

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2020 09:02 AM
what are you getting in log ? did you check the timezone?
i had tested on my personal instance with same time and date in two different field and updated on duration type field, which updated 16 days .