- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2014 04:36 AM
Hi All
I am trying to create a client script which, as soon as a date is entered into a field, it calculates the number of days between that date and today. I have searched the wiki and community and cannot find what I am looking for.
Below is what I have so far, but it keeps coming back with an error. Any help would be much appreciated.
function onChange(control, oldValue, newValue, isLoading) {
var start = nowDateTime();
var end = g_form.getValue('u_date_of_last_recovery_test');
var answer = (start - end);
g_form.setValue('u_days_since_last_tested', answer);
}
Many thanks
Leanne
Solved! Go to Solution.
- Labels:
-
Service Mapping

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2014 07:26 AM
I am pasting the code incase the demo gets reset
Client side code:
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var val = g_for m.getValue('u_date_of_last_recovery_test');//compare this with your field
alert(val);
var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name', val);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
//Type appropriate comment here, and begin script below
}
>>Script include:HelloWorld (check client callable)
Script:
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld: function() {
var c = this.getParameter('sysparm_user_name');
var diff = gs.dateDiff(gs.nowDateTime(), c, true);
return diff;
},
_privateFunction: function() { // this function is not client callable
}
});
Please mark answer as correct if it resolves your issue.
Thanks
Pradeep Sharma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2014 04:51 AM
Hi Leanne,
You can use "dateDiff(String, String, boolean)" function. please refere wiki link for more info
Use this function at script include side and call this at the client side.
I hope this helps
Thanks,
Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2014 05:55 AM
Hi Pradeep
Thanks so much for responding. I am very new to scripting so I'm not really sure what I'm doing! I have tried following the instructions and created the script include below, and ticked the client callable option. Can you please let me know if it is correct? Also, I am not sure how to then call that script from an onchange client script. Can you please assist further? I really appreciate your help.
Regards
Leanne
var DateDiff = Class.create();
DateDiff.prototype = {
initialize: function() {
// Given two date/times as strings
var dateString1 = gs.now;
var dateString2 = g_form.getValue('u_date_of_last_recovery_test');
// Determine the difference as number of seconds (returns a string)
var diffSeconds = gs.dateDiff(dateString1, dateString2, true);
},
type: 'DateDiff'
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2014 07:23 AM
Hi Leanne,
I have done this one demo instance https://demo002.service-now.com/login.do(username admin pass admin) according to your requirement on incident form.
Please check client script "datecal" on incident form and script include "HelloWorld"
Please let me know if you have any issues.
Thanks,
Pradeep Sharma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2014 07:26 AM
I am pasting the code incase the demo gets reset
Client side code:
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var val = g_for m.getValue('u_date_of_last_recovery_test');//compare this with your field
alert(val);
var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name', val);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
//Type appropriate comment here, and begin script below
}
>>Script include:HelloWorld (check client callable)
Script:
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld: function() {
var c = this.getParameter('sysparm_user_name');
var diff = gs.dateDiff(gs.nowDateTime(), c, true);
return diff;
},
_privateFunction: function() { // this function is not client callable
}
});
Please mark answer as correct if it resolves your issue.
Thanks
Pradeep Sharma