Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Work days between two dates - finally!
Message
From
07/06/2003 21:59:26
Mike Yearwood
Toronto, Ontario, Canada
 
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00797684
Message ID:
00797708
Views:
22
Hi Glenn (and anyone else who'd care to jump in!)

No looping at all eh?

What about this?
LPARAMETERS tdStart, tdEnd
LOCAL lnStartOffset, lnEndOffset, lnReturn

*Adjust the first parameter back to the first Sunday.
lnStartOffset = DOW(m.tdStart) - 1

*Adjust the second parameter forward to the following Saturday.
lnEndOffset = 7 - DOW(m.tdEnd)

*Find the difference between the adjusted parameters.
lnReturn = (m.tdEnd + m.lnEndOffset) - (m.tdStart - m.lnStartOffset) + 1

*Determine the number of weekdays.
lnReturn = INT(m.lnReturn * .712845) + 1 && 5 / 7

*Now undo the adjustments.
lnReturn = m.lnReturn - (m.lnStartOffset - 1) - (m.lnEndOffset - 1)

*Coded like this so I can watch lnReturn in the debugger.
RETURN m.lnReturn
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform