Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Fridays in date range (for loop) 3

Status
Not open for further replies.

putts

Programmer
Dec 13, 2002
93
US
I need to figure out how many Fridays fall in a certain date range.

I had a good algorithm for it, but I dont know how to go about making a for loop.

Here's what I had

if {?postDate} = "" then
for i=0 to ((CurrentDate-{?preDate}) mod 7)
if DayOfWeek (DateAdd ("d",i,{?preDate})) = "Friday" then
truncate((CurrentDate-{?preDate})/7,0) + 1
next

truncate((CurrentDate-{?preDate})/7,0)
else
for i=0 to (({?postDate}-{?preDate}) mod 7)
if DayOfWeek (DateAdd ("d",i,{?preDate})) = "Friday" then
truncate(({?postDate}-{?preDate})/7,0) + 1
next

truncate(({?postDate}-{?preDate})/7,0)

So the question is, is there to run a for loop (which I'm guessing there's gotta be) and how do I do it.

TIA
 
You might simplify this, what you're really doing is getting the number of weeks, adjusting for any missing Fridays.

Datediff("w",currentdate,{?preDate}) will return the number of weeks.

Now allow for the start and end dates possibly missing a Friday.

Sorry, I don't have Cryswtal installed right now or I'd work out the formula, but you get the idea.

-k
 
Well it's the second part that I'm having a problem with.

The basis of my algorithm is this:
1. determine the number of full weeks in between my two dates
2. I figure out how many days are leftover after calculating the number of weeks [(difference in dates) mod 7] and figure out if a Friday falls into that leftover time.


For Example,
if the preDate is 04/01/03 and the end date is 04/17/03. The difference between the dates is 16 days which means that there's at least two fridays in there.
The number of days leftover when I subtract the 14 days for the two weeks from the original 16 leaves with 2 days leftover and now I have to figure out if there's a Friday in there.
This is what I have the problem with because this number could be in the range of 1 - 6 days. (thus my for loop)

So, that's where I'm stuck with this.
 
numberVar n;
numberVar i;

n:= DateDiff("d",{?sDate},{?eDate})\7;

for i:= 0 to (DateDiff("d",{?sDate},{?eDate}) Mod 7) do
(
If DayOfWeek (DateAdd("d",-i,{?eDate})) = 6 Then
(
n:= n + 1;
exit for);
);
n
 
Thanks for that loop - I need to do some reading on syntaxes.

I found a way to do it with a downloadable Seagate component:

if (isDate({?preDate}) and isDate({?postDate})) then
cstr(wdaysclass1wdnumworkdays(CDate({?preDate}),CDate({?postDate}),"-----6-"))
else
"Error in date parameters"

Which, as you can see, is very compact and modular, so I'm gonna try to use this.

Thanks for the responses.
 
Here is my formula which works:

datevar firstday:=Date(2002,8,1);
datevar lastday:=Date(2002,8,31);
numbervar loop;
numbervar Days;
numbervar span:=datediff("d",Firstday,LastDay);

For loop:= 0 to span do(
if dayofweek(firstday+loop) = 6 then Days:=Days+1 else Days:=Days);

Days


Software Sales, Training, Implementation and Support for Exact Macola, eSynergy, and Crystal Reports
dgilsdorf@trianglepartners.com
 
Thanks dGillz.

That was perfect.

I'm gonna modify it a bit to calculate some time logs out of a table but I needed that for loop with Fridays to get it going.

Thanks again to everyone for responding.
 
I've used the following in the past to calculate the number of Fridays between 2 dates:

DateDiff ("ww", Date(2003,04,01), Date(2003,04,17), 6)

Just to break it down, "ww" is the number of first day of weeks between the 2 dates, and the 6 at the end makes Friday the first day of the week, hence giving the number of Fridays between 2 dates.


Reebo
Scotland (Sunny with a Smile)
 
Reebo,

A star for you. I did not know that datediff() had a fourth argument.

Software Sales, Training, Implementation and Support for Exact Macola, eSynergy, and Crystal Reports
dgilsdorf@trianglepartners.com
 
Thank you.....I'd like to thank my family and friends who have stood by me all these years...This ones for you Mum!!

Reebo
Scotland (Sunny with a Smile)
 
Reebo, you are a crazy fool. But I like your style. [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top