- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2023 11:10 PM
Hello All,
I am new in ServiceNow Development can anyone please guide me on how to check the difference between the two dates?
my requirement is I want to find the difference between the date of creation of the incident and the date of updation of the incident.
Thank You.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2023 12:17 AM
If you are looking for Date Difference when Incident is Updated, it is more recommended to use After Business Rule on Update with your required Condition. Use below code there:
var duration = gs.dateDiff(current.sys_created_on.getDisplayValue(), current.sys_updated_on.getDisplayValue(), false);
current.<duration_field> = duration;
Still if your situation as to use Client script, you can use below code:
Client Script:
var strt = g_form.getValue('sys_created_on');
var end = g_form.getValue('sys_updated_on');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXML(DateDiff);
function DateDiff(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('<duration_field>', answer);
}
Script Include:
Name: AjaxDarCalc
Client Callable: TRUE/Checked
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
},
});
NOTE: The gs.dateDiff() method is not available in scoped applications.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2023 12:17 AM
If you are looking for Date Difference when Incident is Updated, it is more recommended to use After Business Rule on Update with your required Condition. Use below code there:
var duration = gs.dateDiff(current.sys_created_on.getDisplayValue(), current.sys_updated_on.getDisplayValue(), false);
current.<duration_field> = duration;
Still if your situation as to use Client script, you can use below code:
Client Script:
var strt = g_form.getValue('sys_created_on');
var end = g_form.getValue('sys_updated_on');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXML(DateDiff);
function DateDiff(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('<duration_field>', answer);
}
Script Include:
Name: AjaxDarCalc
Client Callable: TRUE/Checked
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
},
});
NOTE: The gs.dateDiff() method is not available in scoped applications.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2023 02:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2023 05:06 AM
Hi @Abhijeet Pawar ,
please post a screen dump of your calculation together with which fields you are trying to calculate the difference between.
if my answer has helped with your question, please mark as accepted solution and give a thumb up.
Best regards
Anders
If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.
Best regards
Anders
Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2023 05:16 AM
Can you please share the code you have written.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023