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 09:54 AM
I just copy pasted your code in my PDI and I just changed the below lines.
dateTimediff = GlideDateTime.subtract(date1, date2); // You gave the subtract spelling wrong
return dateTimediff.getDisplayValue();
I got the alert message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:27 AM
Hi @tsoct
There is some correction in your script include code. Try this:
dateTimediff = GlideDateTime.subtract(date1, date2);
return dateTimediff.getNumericValue();
Thanks!
Please mark this response as correct and helpful ✔️👍
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:33 AM
Hello,The result is still null and javascript error remained.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:52 AM
try using logs in script include and alert in client script on each line and check the point where your code is breaking.
Also, make sure Client callable checkbox is true in your script include. If not, create a new script include accordingly. Don’t check this checkbox True on the same script include because syntax will not get changed automatically now.
Thanks!
Please mark this response as correct and helpful if it works ✔️👍