- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 09:36 AM
HI All,
I have 3 fields
1. star date
2.end date
3. count days
i want difference between dates in 'count days'
Please help us i need code client script & script include also
Thnaks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 11:42 AM
Hi @basha shaik,
Hope you are doing well.
I tried to find the solution for your scenario or requirement and implement it on my Personal Developer Instance, so that you can easily understand and get the perfect solution in one go.
Solution Proposed
As a solution, you need to create an "On Change - Client Script" and one "Script Include" that will execute the logic or implementation as per the "Last Date" Field changes. Attaching the script that will help you to fulfil your and others requirement via your question asked in the community.
Client Script: -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('global.GetDaysFromDates'); // GlideAjax Call
ga.addParam('sysparm_name', 'getCampus'); // Calling Function
ga.addParam('sysparm_start_date', g_form.getValue("u_start_date")); // Passing Start Date value
ga.addParam('sysparm_end_date', g_form.getValue("u_end_date")); // Passing End Date value
ga.getXML(updateCampus);
function updateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); // Getting Response
if (answer) {
g_form.setValue('u_count_days', parseInt(answer));
}
}
}
Script Include: -
var GetDaysFromDates = Class.create();
GetDaysFromDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCampus: function() {
var start_date = this.getParameter('sysparm_start_date');
var end_date = this.getParameter('sysparm_end_date');
var startDate = new GlideDateTime(start_date);
var endDate = new GlideDateTime(end_date);
var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); // this will return output in seconds
// var diff = GlideDateTime.subtract(startDate, endDate);
var days = diff/(60*60*24); // converting seconds into days
return days;
},
type: 'GetDaysFromDates'
});
For your reference, also attaching screenshots of the outputs that will give you better insights of how this script is working or the best thing will be to follow the solution and execute the script on your instance.
If you find this answer/solution/suggestion as helpful to your question asked or meet with your requirement, please do not forget to mark this solution/answer/suggestion as helpful, and correct.
Thanks 🙂
Aakash Garg
ServiceNow Developer

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 11:05 AM
@basha shaik Please refer to this thread https://www.servicenow.com/community/developer-forum/client-script-date-compare/m-p/1503093 where one of the responders has posted client script as well as script include for the date comparision.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 11:42 AM
Hi @basha shaik,
Hope you are doing well.
I tried to find the solution for your scenario or requirement and implement it on my Personal Developer Instance, so that you can easily understand and get the perfect solution in one go.
Solution Proposed
As a solution, you need to create an "On Change - Client Script" and one "Script Include" that will execute the logic or implementation as per the "Last Date" Field changes. Attaching the script that will help you to fulfil your and others requirement via your question asked in the community.
Client Script: -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('global.GetDaysFromDates'); // GlideAjax Call
ga.addParam('sysparm_name', 'getCampus'); // Calling Function
ga.addParam('sysparm_start_date', g_form.getValue("u_start_date")); // Passing Start Date value
ga.addParam('sysparm_end_date', g_form.getValue("u_end_date")); // Passing End Date value
ga.getXML(updateCampus);
function updateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); // Getting Response
if (answer) {
g_form.setValue('u_count_days', parseInt(answer));
}
}
}
Script Include: -
var GetDaysFromDates = Class.create();
GetDaysFromDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCampus: function() {
var start_date = this.getParameter('sysparm_start_date');
var end_date = this.getParameter('sysparm_end_date');
var startDate = new GlideDateTime(start_date);
var endDate = new GlideDateTime(end_date);
var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); // this will return output in seconds
// var diff = GlideDateTime.subtract(startDate, endDate);
var days = diff/(60*60*24); // converting seconds into days
return days;
},
type: 'GetDaysFromDates'
});
For your reference, also attaching screenshots of the outputs that will give you better insights of how this script is working or the best thing will be to follow the solution and execute the script on your instance.
If you find this answer/solution/suggestion as helpful to your question asked or meet with your requirement, please do not forget to mark this solution/answer/suggestion as helpful, and correct.
Thanks 🙂
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 12:14 PM
Thanks for helping