How to Script n Number of Business Days Ago?

JosephW1
Tera Guru

Hello,

How would I write a Script Include to give me n number of business days ago? I want this script to:

1) operate on a M-F Business Week model,
2) to not count Holidays as business days, and
3) to accept an integer with the function call

My end goal is to be able to build scripted date/time conditions using this new script include.

Examples:

2 Business Days ago, Executed Friday. Result: This Wednesday
2 Business Days ago, Executed Monday. Result: Last Thursday
2 Business Days ago, Executed Tuesday. Result: Last Friday
2 Business Days ago, Executed Tues. (if Mon was a holiday). Result: Last Thursday
8 Business Days ago, Executed Wednesday. Result: 2 Fridays Ago

 

My code below is working for hard-coded dates I pass to it. However, when I remove the hard-coded date it stops working for some reason. Changing to "var gdt = new GlideDateTime();" is resulting in my widget freezing when ran, whereas "var gdt = gs.nowDateTime()" is resulting in no result.

	var gdt = new GlideDateTime('2020-05-26 12:00:00');
	var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
	var n = 3; //positive or negative
	var i = n > 0 ? -1 : 1; //if positive n subtract days, else add
	
	while( n != 0){
		gdt.addDaysLocalTime(i);
		if (sched.isInSchedule(gdt, 'US/Eastern'))
			n = n + i;
	}

How do I fix my code to accept the current GlideDateTime(), or is there a better/more efficient way of scripting this to begin with?

 

Kind Regards,

Joseph

1 ACCEPTED SOLUTION

JosephW1
Tera Guru

 

The reason the script was freezing at one point but not another is because I was passing it a hour that was outside of the chosen schedule, and so n was never reduced to 0 in the while statement.  On the other hand, it would work fine if I passed it an hour that is within the chosen schedule.

Here's my fixed script includes. I'm not sure if this is the best fix/best method to begin with, so please post a better one if you have one. I'll gladly mark your post as the correct answer for this question if you post such.

function bdaysAgoStart(n){
	var gdt = new GlideDateTime();
	var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 Excluding Holidays, floating timezone
	var i = n > 0 ? -1 : 1; //if positive n subtract days, otherwise add
	
	//CALIBRATE TO A BUSINESS HOUR
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 11:00:00");
	
	//PROCESS THE DAYS
	if( sched.isValid() ){ //infinite loop protection
		while( n != 0 ){
			gdt.addDaysLocalTime(i);
			if (sched.isInSchedule(gdt, gs.getSession().getTimeZoneName()))
				n = n + i;
		}
	}

	//CORRECT THE TIME
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 00:00:00");
	
	// RETURN THE DATE
	return gdt;
}
function bdaysAgoEnd(n){
	var gdt = new GlideDateTime();
	var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 Excluding Holidays, floating timezone
	var i = n > 0 ? -1 : 1; //if positive n subtract days, otherwise add
	
	//CALIBRATE TO A BUSINESS HOUR
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 11:00:00");
	
	//PROCESS THE DAYS
	if( sched.isValid() ){ //infinite loop protection
		while( n != 0 ){
			gdt.addDaysLocalTime(i);
			if (sched.isInSchedule(gdt, gs.getSession().getTimeZoneName()))
				n = n + i;
		}
	}
	

	//CORRECT THE TIME
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 23:59:59");

	// RETURN THE DATE
	return gdt;
}

View solution in original post

4 REPLIES 4

DirkRedeker
Mega Sage

Hi

Please check out my example from below (compare the both date examples):

var gdt = new GlideDateTime('2020-05-26 12:00:00');
gs.info(gdt);

var gdt2 = new GlideDateTime(gs.nowDateTime());
gs.info(gdt2);

Give it a shot with the sample of "gdt2", where you put "gs:nowDateTime()" into the "GlideDateTime()" function call.

Remember, that NOW will also NOT have "12:00:00" as time, which may result in "obviously" wrong values, but maybe the calculation is right.

Have a close look at both hints: Time and function call.

Let me know if that solved your issue and mark my answer as correct and helpful

 

BR

Dirk

Thanks Dirk, but I was having a problem with a valid gdt, not with constructing a valid gdt. For whatever reason the rest of my script wasn't liking it - freezing/giving no result - when I use the current gdt, but it was liking the hard-coded ones I gave it.

JosephW1
Tera Guru

 

The reason the script was freezing at one point but not another is because I was passing it a hour that was outside of the chosen schedule, and so n was never reduced to 0 in the while statement.  On the other hand, it would work fine if I passed it an hour that is within the chosen schedule.

Here's my fixed script includes. I'm not sure if this is the best fix/best method to begin with, so please post a better one if you have one. I'll gladly mark your post as the correct answer for this question if you post such.

function bdaysAgoStart(n){
	var gdt = new GlideDateTime();
	var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 Excluding Holidays, floating timezone
	var i = n > 0 ? -1 : 1; //if positive n subtract days, otherwise add
	
	//CALIBRATE TO A BUSINESS HOUR
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 11:00:00");
	
	//PROCESS THE DAYS
	if( sched.isValid() ){ //infinite loop protection
		while( n != 0 ){
			gdt.addDaysLocalTime(i);
			if (sched.isInSchedule(gdt, gs.getSession().getTimeZoneName()))
				n = n + i;
		}
	}

	//CORRECT THE TIME
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 00:00:00");
	
	// RETURN THE DATE
	return gdt;
}
function bdaysAgoEnd(n){
	var gdt = new GlideDateTime();
	var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 Excluding Holidays, floating timezone
	var i = n > 0 ? -1 : 1; //if positive n subtract days, otherwise add
	
	//CALIBRATE TO A BUSINESS HOUR
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 11:00:00");
	
	//PROCESS THE DAYS
	if( sched.isValid() ){ //infinite loop protection
		while( n != 0 ){
			gdt.addDaysLocalTime(i);
			if (sched.isInSchedule(gdt, gs.getSession().getTimeZoneName()))
				n = n + i;
		}
	}
	

	//CORRECT THE TIME
	gdt.setDisplayValue(gdt.toString().split(' ')[0] + " 23:59:59");

	// RETURN THE DATE
	return gdt;
}

Hi 

Thanks a lot for the update and thanks for sharing your outcome.

Good to know, you found a solution.

Have fun

BR

Dirk