How to subtract days from a schedule

JPSS
Tera Contributor

In the RITM table we have a field called dur date

 

i want to subtact 3 working days from thet due date and get the new date. (Thenumber of days might varies)

 

Could any one help.. 

7 REPLIES 7

Louise VA
Tera Contributor

I have used something like this. It consults a schedule that defines business days. So you can also add holidays and such. So for example, running this in my environment (plugging in my schedule on row 6), it returns Friday Feb 23 because today (Feb 16) is a Friday, and it skips Sat, Sun and also Monday because it is a US holiday. 

gs.print(getDateXBusinessDaysAhead(new GlideDateTime(), 4));

function getDateXBusinessDaysAhead(startDate, daysAhead) {
	var inXDays = new GlideDateTime(startDate); // Start date
	var xDays = daysAhead; // Number of business days ahead you want
	var schedule = new GlideSchedule(<sys_id>); //Your schedule defining which days are business days

	var i = 0;
	var j = 0;

	// Loop through days until we have looped through X days in schedule (or looped through total of 60 times, to avoid infinite loops)
	while (i < xDays && j < 60) {

		// Add one day
		inXDays.addDays(1);

		// If day is a day in schedule, increment i
		if (schedule.isInSchedule(inXDays)) {
			i++;
		}

		j++;
	}
    return inXDays.getDate();
}

 

JPSS
Tera Contributor

Hi Ivanal,

 

I could see in  your code you have added days to todays days  based on a calander.

 

i was to have it in the reverse order. Subtract days from todays date excluding week days.

 

Hi @JPSS  to subtract days you just need to add a negative number.

		inXDays.addDays(-1);

If you want to use the OOTB weekday with holidays schedule, you need to change one more line

var schedule = new GlideSchedule( '090eecae0a0a0b260077e1dfa71da828' ); //Your schedule defining which days are business days

no other change is needed for this to work. 

 

Hope this helps

--

Bala Guthy

 

One thing I would add to this is if you are using a glideDate record instead of a glideDateTime record then you would need to add the additional 8-9 hours in seconds or milliseconds to the startDate in order for this to work successfully, otherwise it will continue to loop for all 60 days until the condition for j is met. 


var inXDays = new GlideDateTime();
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
var xDays = 10;
var hours = 8*60*60

inXDays.setDisplayValue(fd_data.flow_var.variable_date);
inXDays.addSeconds(hours)

var i = 0;
var j = 0;

while(i < xDays && j < 60){
   
    inXDays.addDays(-1);

    if(schedule.isInSchedule(inXDays)){
        i++;
    }
    j++
}

return inXDays.getDate();