The CreatorCon Call for Content is officially open! Get started here.

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

Pranesh072
Mega Sage

try this 

 

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();
	ed.addDays(this.getParameter('sysparm_days')); //10 days from now

	var diff = ed.before(gdt);

	return diff;

}

Hi @Pranesh What is this supposed to do? 

It only calculates 10 WORKING days, not 10 BUSINESS days from now on.

It actually works correctly, but so does mine code above.

I am looking for a solution to calculate 10 business days from today's date...

Oh, sorry i interpreted something else.

 

You have to use schedule and GlideSchedule API

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideScheduleS...

Dear @Pranesh 

It's quite hard for me to modify the script myself, that is why I kindly asked for help here..