- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 08:38 AM
Hi,
I have a requirement to count days excluding weekends. I have a date range for an example.
12/Jan/2022 - 25/Jan/2022, returns 9 days where as it return 10 days.
Below is the code that i am using
Client Script:
var fromDate = g_form.getValue("from_date");
var toDate = g_form.getValue("to_date");
var ajax = new GlideAjax('CalDiffDateIteration');
ajax.addParam('sysparm_name', 'durCalc');
ajax.addParam('sysparm_fromDate', fromDate);
ajax.addParam('sysparm_toDate', toDate);
ajax.getXML(returnIterationDays);
function returnIterationDays(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = parseInt(answer);
g_form.setValue(setItrNum, answer);
}
Script Include:
var CalDiffDateIteration = Class.create();
CalDiffDateIteration.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
durCalc: function() {
var fromDate = new GlideDateTime(this.getParameter('sysparm_fromDate'));
var toDate = new GlideDateTime(this.getParameter('sysparm_toDate'));
var days = getDateDiffExcWeekends(fromDate, toDate);
return days;
function getDateDiffExcWeekends(start, end) {
gs.info("Script - start:" + start);
gs.info("Script - end:" + end);
var days = 0;
while (start < end) {
start.addDaysUTC(1);
if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7) {
days++;
}
}
gs.info("Script - days:" + days);
return days;
}
}
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 11:33 PM
Hi,
I used this and it gave me 10 days
var startDateGdt = new GlideDateTime('2022-01-12 00:00:00');
var endDateGdt = new GlideDateTime('2022-01-25 00:00:00');
var days = getDateDiffExcWeekends(startDateGdt,endDateGdt);
gs.info(days);
function getDateDiffExcWeekends(start,end){
var days = 0;
while (start < end) {
start.addDaysUTC(1);
if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7){
days++ ;
}
}
return days+1;
}
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 01:13 PM
Hi,
In your script you start by adding a day, before checking the if-statement and add the day to your variable, so in effect the first day is skipped.
By just adding a gs.info, you can see how the counter acts.
var start = new GlideDateTime('2022-01-12');
var end = new GlideDateTime('2022-01-25');
var days = 0;
while (start < end)
{
start.addDaysUTC(1);
if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7)
{
gs.info('start: ' + start);
days++;
}
}
gs.info('total days: ' + days);
/*
OUTPUT
*** Script: start: 2022-01-13 00:00:00
*** Script: start: 2022-01-14 00:00:00
*** Script: start: 2022-01-17 00:00:00
*** Script: start: 2022-01-18 00:00:00
*** Script: start: 2022-01-19 00:00:00
*** Script: start: 2022-01-20 00:00:00
*** Script: start: 2022-01-21 00:00:00
*** Script: start: 2022-01-24 00:00:00
*** Script: start: 2022-01-25 00:00:00
*** Script: total days: 9
*/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 11:20 PM
Hi,
Can you please help me on how to over come this? If the remove the start.addDaysUTC(1); line from the code it runs into a loop.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 12:44 AM
You can adjust by setting your initial variable to one instead, or as
var days = 1;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 11:33 PM
Hi,
I used this and it gave me 10 days
var startDateGdt = new GlideDateTime('2022-01-12 00:00:00');
var endDateGdt = new GlideDateTime('2022-01-25 00:00:00');
var days = getDateDiffExcWeekends(startDateGdt,endDateGdt);
gs.info(days);
function getDateDiffExcWeekends(start,end){
var days = 0;
while (start < end) {
start.addDaysUTC(1);
if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7){
days++ ;
}
}
return days+1;
}
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader