Still trying to set a Date field using the @BusinessDays function

OK this is till an issue, some how it is working at times, so I am doing something wrong here,

I have a ReceivedDate field and from that date I need to add NumDays then I need to set the DueDate field, I need to skip the weenends and the Stat Holidays

So if I have 2 feb, 2009 and I add 5 days I should get 9 feb,2009. Instead I get 8 Feb, 2009 (a Sunday) it should skip the weekends.

On the other hand if I add 4 to 2 feb, 2009 I get 5 feb,2009 (of course I starts to count 4 from the ReceivedDate )

Again if I add 6 days to the ReceivedDate being 2 feb, 2009 then I get 9 feb, 2009, that again it is good…

The StatHolidays are 2008-09-01, 2008-10-13, 2008-11-11, 2008-12-25, 2008-12-26, 2009-01-01

If I test the StatHolidays;

I add 5 days to the 10 Nov, 2008 and I get the 17 Nov, 2008 (it is working as it is not counting the weekend and the statHoliday on the 11 Nov 2008).

I add 4 days to the 10 Nov, 2008 and I get the 16 Nov, 2008 (it is NOT working the 16 is a weekend.).

FIELD NumDays := NumDays;

FIELD ReceivedDate := ReceivedDate;

FIELD DueDate := DueDate;

Received := ReceivedDate;

@For( x := 1;

@BusinessDays( Received ; @Adjust(Received;0;0;x;0;0;0) ; 1:7; @TextToTime(StatHolidays)) <= NumDays;

x := x +1; num := x);

DueDate := @Adjust(Received;0;0;num;0;0;0);

@If( DueDate =“”; @SetField(“DueDate”;DueDate);“”)

Thanks,

Alena

Subject: Still trying to set a Date field using the @BusinessDays function

Your condition statement:

@BusinessDays(@Adjust(Received;0;0;x;0;0;0) ; 1:7; @TextToTime(StatHolidays)) <= NumDays

is true on Friday, but it is still true on Saturday and Sunday – it isn’t until Monday that another business day is added and the condition goes false, allowing the loop to exit. The formula would be a lot cleaner as:

due_date := ReceivedDate;

@While(@BusinessDays(due_date ; 1:7; @TextToTime(StatHolidays)) < NumDays;

@Adjust(due_date; 0; 0; 1; 0 ;0; 0));

FIELD DueDate := due_date

Subject: Fixed-Still trying to set a Date field using the @BusinessDays function

Found some code on this site,

holidays := @Explode(@TextToTime(StatHolidays));

date := fldReceivedDate;

origdate := date;

increment:= NumDaysToAddNum;

origincrement := increment;

@For(n := 1;

n <= increment;

n := n + 1;

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

@If(

@Weekday(date) = 1 | @Weekday(date) = 7 | @IsMember(@Text(date); holidays);

increment := increment + 1; “”));

DueDate := @Adjust(origdate; 0;0; increment;0;0;0);

Subject: Still trying to set a Date field using the @BusinessDays function

Ah I see the problem with the original code:

@BusinessDays( Received; @Adjust( Received; 0;0; x; 0;0;0); 1:7; StatHolidays ) <= NumDays

If the last day happens to fall on a Friday (or before a holiday) when the system continues to determine if it can add another business day it tries the next day. In the case of a Friday: Saturday, we still haven’t gone past NumDays then it tries Sunday and were still OK.

Then when we hit Monday (assuming it’s not a Holiday). The number of Days is greater than the value we want. We exit the loop and X is set to one less day than we tried. Clearly this would be the Sunday, which is NOT what you want.

I would code it so instead of trying everything until we go past the date. I would loop until we get exactly what we want

@if( NumDays <=0; @Return(""); "");

x := NumDays - 1;

@While( @BusinessDays( 

		Received; 

		@Adjust( Received; 0;0; x; 0;0;0); 

		1:7; 

		StatHolidays ) <> NumDays;

	x:= x + 1

);

Field DueDate := @Adjust( Received; 0;0; x; 0;0;0);

I made it NumDays - 1 initially is because it’s clear by your posting, you’re counting the Received date as part of you count: (I would have thought adding 5 days to the 10 Nov, 2008 (with Nov 11 being a holiday) that Nov. 18 would be the right value. But you said Nov. 17 was right. )

If Nov. 18 IS right then you probably want to set it as follows:

@if( NumDays <=0; @Return(""); "");

x := NumDays;

@While( @BusinessDays( 

		Received; 

		@Adjust( Received; 0;0; x; 0;0;0); 

		1:7; 

		StatHolidays ) <> (NumDays+1);

	x:= x + 1

);

Field DueDate := @Adjust( Received; 0;0; x; 0;0;0);

In this example you need to be careful you don’t get into an infinte loop. Therefore Either x must be 1 less than NumDays or Numday 1 more than the starting point.

I like setting x := Numday (- 1); instead of setting it to 1 because if the Numdays = a large number (say 1000) we are doing a lot more work than we need too.

For 1000:

Starting from x=1 - we’d loop 1000 + (52 * 2)*3 + (Number of Holidays)*3 times.

about 1350 

Starting from 1000 we cut that to:(52 * 2)*3 + (Number of Holidays)*3 times.

about: 350

You could use x = 1 if you know you’re NumDays is always small.