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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Weekdays

Status
Not open for further replies.

dazf

Programmer
Jun 26, 2002
2
GB
I am trying to create a formula to count the number of weekdays(Mon-Fri) in a date range. My effort so far is:

Local NumberVar Weekdays :=0;
Local NumberVar index ;
for index := {DBTable.Date1} to {DBTable.Date2} do
(
if(DayOfWeek(index)in 2 to 6 )then
Weekdays := Weekdays + 1
);
Weekdays

This doesn't work as a FOR loop requires an interger and not a date field to count.

Has anybody any better ideas? Thanks.
 
Try:

for index = 1 to datediff("d",{date1},{date2})+1

And you should be fine Software Training and Support for Macola, Crystal Reports and Goldmine
714-348-0964
dgilsdorf@mchsi.com
 
Here is the formula I use. It's very similar to yours except for how I calculate the number of loops. Your's also didn't add the "index" number to the beginning date. You results would end up being either zero or the number of loops.

datevar firstday:={DBTable.Date1} ;
datevar lastday:= {DBTable.Date2} ;
numbervar span;
numbervar loop;
numbervar wd //short for weekdays
span:=lastday-firstday;

For loop:= 0 to span do
(
if dayofweek(firstday+loop)in [2 to 6] then wds:=wds+1 else wds:=wds);

wds


One "fix" for dgillz's method would be to change it to:
for index:= 0 to datediff("d",{date1},{date2})+1



Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top