- 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
06-03-2014 03:33 AM
Hi solutioner now,
I did look into that, but need to get the data from the following.
I need to take one date from "purchase_date", another date from "u_repair_date" and then display the difference in "u_date_difference"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2014 05:01 AM
Hi Alex,
Here is the updated script
.
You can use it as follows.
// Given two date/times as strings
var dateString1 = current.purchase_date;
var dateString2 = current.u_repair_date;
// Determine the difference as number of seconds (returns a string)
var diffSeconds = gs.dateDiff(dateString1, dateString2, true);
current.u_date_difference = diffSeconds;
Please mark this answer as Correct/Helpfull if it was really helpful for you.
Regards,
Solutioner
Enhance Knowledge NOW@ www.solutioningnow.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2014 05:10 AM
Thanks,
Problem is, i cannot use current. or gs. in a client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2014 05:17 AM
You can use newValue, oldValue, or make a thing like this :
var inc = new GlideRecord('u_your_table');
inc.addQuery('u_your_field');
inc.query();
while (inc.next())
jslog('Data = " + inc.your_field);
or use g_form like this :
var myValue = g_form.getValue('your_field');

- 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);
}
});