- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2014 03:04 AM
Hi All,
I have two seperate dates in the format of DD-MMM-YYYY that i need to calculate the difference off. I need the answer to be in days. What's the best way to achieve this as i'm using a client script i cannot use anything gs.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2014 05:17 AM
Hi Alex,
Try this
Client script:
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue('<start_field>');//set this as purchase date
var end = g_form.getValue('<end_field>');//set this as repair date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
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, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2016 11:55 AM
Hi pradeepksharma,
This is not working in my Scoped App. Its bcoz of getXMLWait(), I have tried using getXML(), getXML(callback) as well as suggested in some pages. Any help on this???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2016 01:30 PM
Brand new to SNow, thanks for this script, it sets the duration field (u_duration) I created just fine, however, there is an unexpected issue that I can't figure out.
When the duration is set, it seems that the 'Name' of the change is altered.
In the change list, you see 'CHG000368' but when you open it, the tab header is the duration (i.e. '1 Day') and the filter on the Change Tasks tab says 'Change Request = 1 Day
Also the notifications that go out contain text like "Change Request '1 Day' Approval Request" and the task name is 'Click Here to Approve 1 Day'
Where so I start looking for why this is happening?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 08:37 AM
Pradeep,
Is there something I can add so it returns only the day?
Right now I am getting "4 00:00:00"....I only need the 4 since my fields are only date fields. I don't need the time at all.
EDIT: Disregard...I changed your script include gs.datediff to "true" which gave me seconds and then I divided that by 86,400 to get days.
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2018 05:05 PM
Hello Pradeep,
Can you please help me with this thread which is similar to this thread.
https://community.servicenow.com/community?id=community_question&sys_id=bf8b249cdb4f9b04e0e80b55ca9619c6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2019 04:33 PM
Hi Pradeep,
Will it be working even in scoped app for using 'GlobalAbstractAjaxProcessor' ?