Populate duration difference of two date/time type variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 08:10 AM - edited 11-17-2023 09:36 AM
Hi everyone,
I attempted to populate the time difference in the duration field in the catalog item, but nothing came up. What might have gone incorrectly?
//Script include
getDuration: function() {
var dateTimediff;
var date1 = new GlideDateTime();
var date2 = new GlideDateTime();
var fromdate = this.getParameter('sysparm_from_date');
var todate = this.getParameter('sysparm_to_date');
date1.setValue(fromdate);
date2.setValue(todate);
dateTimediff = GlideDateTime.substract(date1, date2);
return dateTimediff.getDurationValue();
},
// Catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue('u_duration_from');
var end = g_form.getValue('u_duration_to');
var ajax = new GlideAjax('THERequestUtilsAjax');
ajax.addParam('sysparm_name', 'getDuration');
ajax.addParam('sysparm_from_date', strt);
ajax.addParam('sysparm_to_date', end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
alert('Duration: '+answer); //this return null
g_form.setValue('expected_duration', answer);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 08:21 AM
Hi,
Can you replace
dateTimediff = GlideDateTime.substract(date1, date2);
with
dateTimediff = GlideDateTime.subtract(date1, date2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 08:36 AM
Thanks but i am still getting null on the alert. Also, an error msg when form load
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:08 AM
Hi @tsoct
In script include, can you try like below:
return dateTimediff.getDisplayValue();
Regards,
Vasu Ch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:20 AM
hello Vasu,
Same where it return null