Calculating business days to a date

I have found this code on the web, this will remove the weekend form the date, but what I need is to be able to add a number of days say 34 (working days) to today’s date. I need to add the weekends to the Estimated date, who can I so this? I cannot use the @businessdays function, I am using R5.

Dayslater := @adjust(@today;0;0;x;0;0;0);

Weekday := @weekday(dayslater);

@If(weekday=7;@adjust(dayslater;0;0;2;0;0;0);

@if(weekday=1;@adjust(dayslater;0;0;1;0;0;0);@today))

this code gives me the date minus the weekend… so if I add 16 working days to today I should get 23 march, but this code gives me 19 march……

how can I do this?

Thanks

p.

Subject: calculating business days to a date.

Take the number of business days divided by 5 and return it as an integer. That will tell you the number of weeks (34 / 5 = 6). Multiply the weeks by 7 to give you the first adjustment days (6 * 7 = 42). In part 2, you take the Modulo of the first equation, @Modulo(34;5), to get the number of additional days to adjust (4). If the date after the second adjustment lands on a weekend, move it to the following Monday.

If you want to consider holidays you will need to add even more logic and by then you will be better off going to lotusscript.

Subject: RE: calculating business days to a date.

thanks…

Take the number of business days divided by 5 and return it as an integer. That will tell you the number of weeks (34 / 5 = 6). Multiply the weeks by 7 to give you the first adjustment days (6 * 7 = 42). In part 2, you take the Modulo of the first equation, @Modulo(34;5), to get the number of additional days to adjust (4). If the date after the second adjustment lands on a weekend, move it to the following Monday

x=20

dayslater := @Adjust(@Today;0;0;x;0;0;0);

Weekday := @Weekday(dayslater);

NumOfDays :=@If(weekday=7;@Adjust(dayslater;0;0;2;0;0;0); @If(weekday=1;@Adjust(dayslater;0;0;1;0;0;0);@Today));

NumberOfBusiness := x/5;

AdjustmentDays := NumberOfBusiness *7;

ModuloDays := @Modulo(x;5);

@Adjust(NumOfDays;0;0;ModuloDays;0;0;0)

I am not getting the right answer, what am I doing wrong.

the answer to this shows me march 20,2007 and it should be march 23, 2007

thanks

p.

Subject: RE: calculating business days to a date.

You have all the right parts, just the order of things seems really odd. Given Charles algorithm, I would expect the code to look something like this:

REM { x can be any number of days };

x=20;

NumberOfBusiness := @Integer(x/5);

AdjustmentDays := NumberOfBusiness *7;

ModuloDays := @Modulo(x;5);

dayslater := @Adjust(@Today;0;0; (AdjustmentDays + ModuloDays) ;0;0;0);

weekday := @Weekday(dayslater);

@If(weekday=7;@Adjust(dayslater;0;0;2;0;0;0);weekday=1;@Adjust(dayslater;0;0;1;0;0;0);dayslater);

Give that a try.

Subject: RE: calculating business days to a date.

If the total days is 7So x = 7

Then I get march 12 that is good by if the x = 8 I sill get march 12, if x = 9 again I get march 12 Now if x = 10 then I get march 15, and that is good…. So if x = 8 or 9 it is not working right….

If x =13 again it is wrong…

Any ideas?

Subject: RE: calculating business days to a date.

Yep, I forgot to take into account we are late in the week and we may hit another weekend.

So here are tested versions of both sets of code: (they give the same answer)

Charles Algorithm:

REM { x can be any number of days };

x:=8;

dow := @Weekday(@Today);

NumberOfBusiness := @Integer(x/5);

AdjustmentDays := NumberOfBusiness *7;

ModuloDays := @Modulo(x;5);

bonus := @If(dow+ModuloDays>6;2;0);

dayslater := @Adjust(@Today;0;0; (AdjustmentDays + ModuloDays+bonus) ;0;0;0);

weekday := @Weekday(dayslater);

@If(weekday=7;@Adjust(dayslater;0;0;2;0;0;0);weekday=1;@Adjust(dayslater;0;0;1;0;0;0);dayslater)

My Algorithm tested and fixed:

tday := @Today;

busdays := 8;

@While(busdays > 0;

@do(

			tday := @Adjust(tday;0;0;1;0;0;0);

			@If( 	@Weekday(tday) > 1 & @Weekday(tday) < 7;

					busdays := busdays - 1;

					busdays := busdays

			)

)

);

tday

Charles is better as it’s just a direct calculation, vs my looping approach.

Subject: RE: calculating business days to a date.

that is greate… thanks to all of you… It works.

Subject: RE: calculating business days to a date.

If you count 20 business days from today, Mar 1, you get Mar 29. By formula if x = 20,

20/5 = 4, then 4 *7 = 28, adjust today by 28 days, get Mar 29. There is no modulo @Modulo(20;5), so there is no need to perform a part 2.

Subject: RE: calculating business days to a date.

x :=8;NumberOfBusiness := @Integer(x/5);

AdjustmentDays := NumberOfBusiness *7;

ModuloDays := @Modulo(x;5);

dayslater := @Adjust(@Today;0;0; (AdjustmentDays + ModuloDays) ;0;0;0);

weekday := @Weekday(dayslater);

@If(weekday=7;@Adjust(dayslater;0;0;2;0;0;0);weekday=1;@Adjust(dayslater;0;0;1;0;0;0);dayslater)

if x = 8 or 9 is gives an error… is the @modulo function working right?

p.

Subject: RE: calculating business days to a date.

@Modulo is working fine. End result of this algorithm seems improper. Check for different days.

Subject: RE: calculating business days to a date.

Much smarter answer than mine.

Subject: calculating business days to a date.

How about this (I just tried it in the computed field):

busdays := 34;

tday := @Today;

@While(busdays > 0;

. . . .@If( . .@Weekday(tday) = 1;

. . . . . . . . . . . .tday := @Adjust(tday;0;0;1;0;0;0);

. . . . . . . .@Weekday(tday) = 7;

. . . . . . . . . . . .tday := @Adjust(tday;0;0;1;0;0;0);

. . . . . . . . . . . .@Do(

. . . . . . . . . . . . . .busdays := busdays - 1;

. . . . . . . . . . . . . .tday := @Adjust(tday;0;0;1;0;0;0)

. . . . . . . . . . . .)

. . . .)

);

tday

Don’t use the above, it does not check the day you land on. So if you end up on a Saturday, you get a date of Saturday. I’ve posted a corrected version of my code, plus Charles has a better solution, see this document instead ==>