The CreatorCon Call for Content is officially open! Get started here.

Populate duration difference of two date/time type variables

tsoct
Tera Guru

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?

 

expected_duration (variable type = duration)
u_duration_from (variable type = date/time)
u_duration_to (variable type = date/time)

 

 

 

 

//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);
}

 

 

 

8 REPLIES 8

@tsoct 

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

Vasuch_0-1700243663985.png

 

 

Shubham Singh
Mega Guru

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 ✔️👍

 

Hello,The result is still null and javascript error remained.

@tsoct 

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 ✔️👍