addDaysUTC() is not counting current day

Hari1
Mega Sage

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;
        }
    }
});
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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:

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

OlaN
Giga Sage
Giga Sage

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

*/

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

You can adjust by setting your initial variable to one instead, or as @Ankur Bawiskar suggested, by returning the extra day at the end.

var days = 1;

Ankur Bawiskar
Tera Patron
Tera Patron

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:

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader