Issue in GlideDate format in scoped application

sasikanth1
Tera Contributor

Hi @Ankur Bawiskar ,

 

We have a requirement where we need to calculate the difference between two date/time fields and the answer should populate in another duration field

 

Here, we are using client script and script include

In client script we are getting the values of the date/time values as (dd/mm/yy hh:mm:ss) eg:start date29/06/2022 13:24:12, end date: 31/10/2022 13:24:12 format but when in script include it is taking as (yyyy/mm/dd 00:00:00) eg: 2022-06-29 00:00:00, 2022-10-31 00:00:00

 

But when i tried to change the format of start date on form as " 06/29/2022 13:24:12" then it is taking as "2022-06-29 13:24:12" and populating the exact duration value in duration field

 

Note: We are using GlideDate time in script include and we are calculating the difference of start date and end date as "GlideDateTime.Subtract(start date, end date)"

8 REPLIES 8

Hi @Ankur Bawiskar 

 

Thanks for quick around ankur, Please find the below client script and script include

we are using the fields strt_date/en _date using "glide_date_time" type and duration field type is "glide_duration"

client script:

var strt = g_form.getValue('strt_date');

var end = g_form.getValue('en_date');

if(strt != ' ' &&end !=' ' && newValue != ' '){

var ajDur = new GlideAjax('scriptinclude');

ajDur.addParam('sysparm_name', 'cal');

ajDur.addParam('sysparm_start', strt');

ajDur.addParam('sysparm_end', end);

ajDur.getXMLAnswer(function(answer)){

g_form.setValue('duration', answer);

});

 

}else {

g_form.setValue('duration', ' ');

}

}

 

Script include:

cal : function(){

var startDate = GlideDateTime(this.getParameter('sysparm_start'));

var endDate = GlideDateTime(this.getParameter('sysparm_end'));

var dur = GlideDateTime.subtract(startDate, endDate);

return(dur);

},

@sasikanth1 

update as this

Client Script:
var strt = g_form.getValue('strt_date');
var end = g_form.getValue('en_date');
var dateTimeFormat = g_user_date_time_format;

if(strt != ' ' &&end !=' ' && newValue != ' '){

var ajDur = new GlideAjax('scriptinclude');
ajDur.addParam('sysparm_name', 'cal');
ajDur.addParam('sysparm_start', strt);
ajDur.addParam('sysparm_format', dateTimeFormat);
ajDur.addParam('sysparm_end', end);
ajDur.getXMLAnswer(function(answer){
g_form.setValue('duration', answer);
});
}

Script include:

cal : function(){

var format = this.getParameter('sysparm_format');
var startDate = GlideDateTime();
startDate.setDisplayValue(this.getParameter('sysparm_start'), format);

var endDate = GlideDateTime();
endDate.setDisplayValue(this.getParameter('sysparm_end'), format);

var dur = GlideDateTime.subtract(startDate, endDate);

return(dur);

},

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

I tried using your script but again it is throwing as null value 

@sasikanth1 

what debugging did you perform?

share the logs you added

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader