How to Find the Number of Weekdays Between Two Dates

Last reviewed: April 30, 1996
Article ID: Q114354
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0
  • Microsoft FoxPro for Windows, versions 2.5x, 2.6x
  • Microsoft FoxPro for MS-DOS, versions 2.5x, 2.6x
  • Microsoft FoxPro for Macintosh, versions 2.5x, 2.6a

SUMMARY

This article describes how to programmatically determine the number of weekdays between and including a start date and an end date.

MORE INFORMATION

Code Example

   *program to return the number of weekdays between two dates
   *beginning with d1 through d2
   PARAMETERS d1,d2
   IF (TYPE("d1")="D" AND TYPE("d2")="D")   && both variables present?
      wd=0   && variable to hold total number of weekdays
      FOR D=0 TO d2-d1
         IF BETWEEN(DOW(d1+D),2,6)   && 2=Mon 6=Fri
            wd=wd+1
         ENDIF
      ENDFOR
      RETURN wd
   ELSE
      RETURN 0
   ENDIF

Save the program as WEEKDAYS.PRG. To call it, issue the command:

   ? WEEKDAYS({08/16/61}, {12/31/61})

The answer displayed in the active window should be 98.

NOTE: This program doe not check for invalid dates, such as {2/30/61}.


Additional reference words: VFoxWin 3.00 FoxMac FoxDos FoxWin 2.50 2.50a
2.50b 2.50c 2.60 2.60a
KBCategory: kbprg kbcode
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 30, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.