How to calculate 10 BUSINESS days from today's date?

Melinda7
Kilo Expert

Can anyone please advise about how to calculate a certain number of BUSINESS days from today's date?

USE CASE:

Phone Decommissioning: I need to use a 'date' variable where user needs to add a date that is not earlier than 10 business days from today.

I would need to create a script include function that I can then pull in a GlideAjax.

At the moment, I am using this utility function to calculate the calendar days and it works correctly, if wanting to calculate ONLY calendar days, not BUSINESS days:

isSelectedDateAfterCertainDays: function() {

        if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
            return false;
        }

        var gdt = new GlideDateTime(gs.nowDateTime());
        var selectedDate = this.getParameter('sysparm_selectedDate');
        var ed = new GlideDateTime(gdt);
        ed.addDays(this.getParameter('sysparm_days'));

        var diff = gs.dateDiff(ed, selectedDate, true);

        if (diff > 0)
            return 'true';
        else
            return 'false';

    },

Went through numerous articles here, even the long-running helpful article by MB found here, but honestly, being a coding beginner, I would very much appreciate help with creating a script include function that would calculate the date EXCLUDING the weekends....

Can anyone help, please?

1 ACCEPTED SOLUTION

i updated the code with glide schedule. Make sure to create a valid scheduled cmn_scheduled table for weekdays and pass that sys_id into glideschedule

 

isSelectedDateAfterCertainDays: function() {

	if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
		return false;
	}


	var selectedDate = this.getParameter('sysparm_selectedDate');
	var gdt = new GlideDateTime(selectedDate);

	var ed = new GlideDateTime();
	var days =this.getParameter('sysparm_days')
	var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
	var schedule = new GlideSchedule('38fa64edc0a8016400f4a5724b0434b8'); //sys_id of schedule cmn_schedule table
	var end = schedule.add(ed, dur);

	var diff = end.before(gdt);

	return diff;

}

View solution in original post

12 REPLIES 12

Hi @Lunatic Zorro 

I added the script as you advised. Saved the weekday schedule in a system property, as I am not allowed to use directly a sys_id in the script:

isSelectedDateAfterCertainBusinessDays: function() {

    if (JSUtil.nil(this.getParameter('sysparm_selectedDate')) || JSUtil.nil(this.getParameter('sysparm_days'))) {
        return false;
    }

    var gdt = new GlideDateTime(gs.nowDateTime());
    var selectedDate = this.getParameter('sysparm_selectedDate');
    var schedule = new GlideSchedule();
    var weekdaySchedule = gs.getProperty('client.weekdaySchedule');
    schedule.load(weekdaySchedule);  
    var duration = schedule.duration(gdt, selectedDate);
    var diff = duration.getNumericValue(); //Get answer in ms  

    if (diff > 10 * 24 * 60 * 60 * 1000) // 
        return 'true';
    else
        return 'false';

},

 

Then this is how I am calling it in the catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    g_form.hideFieldMsg('decommission_date', 'true');

    var ga = new GlideAjax('Client_DateTimeUtils');
    ga.addParam('sysparm_name', 'isSelectedDateAfterCertainBusinessDays');
    ga.addParam('sysparm_selectedDate', newValue);
    ga.addParam('sysparm_days', 10);
    ga.getXML(PopulateResult);

    function PopulateResult(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'false') {
            g_form.setValue('decommission_date', '');
            getMessage('provideFutureDate_10businessDays', function(msg) {
                g_form.showFieldMsg('decommission_date', msg, 'error');
            });
        }
    }
}

 

Using a getMessage, because the message has to appear according to user's set language.

 

However, this still does not work. It still only counts 10 working days, not business days...

Can you please correct me, where I got this wrong?

priyanshij
Kilo Guru

Hi Linda,

 

Kindly refer to the below link for calculating business days:

 

https://community.servicenow.com/community?id=community_article&sys_id=df6464c9db1ed4909e691ea668961902

 

Regards

Priyanshi

Hi @priyanshij 

Thank you very much for this helpful article, I browsed through plenty of them, but did not find/see this one.

Kind regards,

Linda