Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Work days between two dates - finally!
Message
From
09/06/2003 02:45:23
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00797684
Message ID:
00797884
Views:
12
>I needed a solution to determine the number of workdays between two dates and have not been satisfied with the prevalent techniques. The ONLY reliable method required looping, and if the dates were very far apart it would have an impact on performance, There were several calculation methods sugested but when pressed all were found to produce errors.
>
>I think I have discovered a method which will reliably produce the correct result;
>
>LPARAMETERS dDate1, dDate2
>LOCAL LD, hd
>LD = MIN(dDATE1,dDATE2)
>hd = MAX(dDATE1,dDATE2)
>return((WEEK(hd,0,2) - WEEK(ld,0,2)) *5) + ;
>    IIF(DOW(ld,2) < 6,6-DOW(ld,2),0) + ;
>    IIF(DOW(hd,2) < 6,DOW(hd,2)-5,0)
>
>
>Please let me know if this technique fails.
>
>Thanks
>
>Glenn

What was the error in Message #791711 ?
Never mind found and corrected :
lparameters td1, td2, tnWorkDaysInWeek
* First day of workday is Monday - 2
Local lnDays
If td2 < td1
	Local ldTemp
	ldTemp = td1
	td1 = td2
	td2 = ldTemp 
endif

tnWorkDaysInWeek = iif(empty(tnWorkDaysInWeek),5,tnWorkDaysInWeek)
lnFullWeeks = floor((td2-td1)/7)
td1 = td1+lnFullWeeks*7
If Dow(td1,2) > tnWorkDaysInWeek and Dow(td2,2) > tnWorkDaysInWeek
	td1 = td1 + Abs(Dow(td1,2)-Dow(td2,2))
endif
lnAdd = Iif( Dow(td1,2) > tnWorkDaysInWeek, 0, 1 )

lnDays = Iif( Dow(td1,2) > Dow(td2,2), ;
	7 - lnAdd - Dow(td1,2) + Dow(td2,2), ;
	lnAdd + Min(Dow(td2,2),tnWorkDaysInWeek) ;
	- Min(Dow(td1,2),tnWorkDaysInWeek) )
return  lnFullweeks * tnWorkDaysInWeek + lnDays
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform