On change client script to calculate the difference in start date and end date and display the result in another field no_of_days in days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2020 03:52 AM
I have two fields start date and end date for my table.I wanted to write an onchange client script such that upon selection of both dates the difference in dates should be auto populated in no-of_days field which is of type integer.
For eg:If start date is 09-09-2020 and end date is 11-09-2020 The no_of_days field should have 3.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2020 03:58 AM
Hello Dipika,
Please check and Try this
Client script:
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue('<start_field>');//set this as start date
var end = g_form.getValue('<end_field>');//set this as end date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','calculate');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('<duration_field>', answer);
}
Script include
Name:AjaxDurCalc
Client callable:checked,client:checked
script:
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculate: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});
Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Regards
Yash.K.Agrawal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 12:27 AM
Hello Deepika,
Did you try the solution i provided, If my solution helped with marked it correct and helpful.which also helps others.
And please keep posted,if wants more help
Regards,
Yash K.Agrawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2023 11:16 AM
helped me. thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2023 03:33 AM
This code is worked for Global scope
when i tried the same script for scoped application, it's not working please help to provide the (scripts ) functions which needs to be used for scoped application.
Thanks in Advance