I want to find the difference between the two date time fields and need to update in other duration field

venkataci
Kilo Contributor

I want to find the difference between the two date time fields and need to update in other duration field.

 

find_real_file.png   Log in Time and Logoff time are Date/ Time fields and Total Log time is duration field.

 

I have written server side script for this to work calling a glide ajax call. But it is not calculating the difference.

 

Client script:

find_real_file.png

 

 

Script include is:

 

find_real_file.png

 

Please help me to resolve it.

 

 

Thanks in Advance.

40 REPLIES 40

Which times do you want in 24 hour format? The value should be as per your system set time format but you could try changing your g and g1 variables to getDisplayValue() rather than getValue().

 

If it's working now please mark an answer correct to close the thread down.

find_real_file.png

Did you try to change the field name of "u_log_off_time" to "u_logoff_time"?? 

Also. Please change scipt include code to the below code. 

return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);

I have tried it in my demo instance and it working absolutely fine.

find_real_file.png

Mark post as correct and helpful.

Got it..did not chkd ...

Try with below code,

In Client script,

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var g = g_form.getValue("u_log_in_time");
var g1 = g_form.getValue("u_log_off_time");

var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',g);
ajax.addParam('sysparm_end',g1);
ajax.getXML(GetAnswer);

function GetAnswer(response) {
var answer= response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('u_total_log', answer);
}


}

 

In script include,

var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

durCalc:function(){

var start=new GlideDateTime(this.getParameter('sysparm_strt'));
var end=new GlideDateTime(this.getParameter('sysparm_end'));

var dur = GlideDateTime.subtract(start,end);
gs.info("duration "+dur.getDisplayValue());

return dur;
},
type: 'AjaxDurCalc'
});