INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Usefull Functions & Procedures

How to count working days by BlindPete
Posted: 28 Nov 00 (Edited 12 May 03)

Originally written in VB by Michael Red (SEE ACCESS FAQ)
Adapted from VB to VFP by Blindpete

I found this function in ACCESS FAQ.  I thought it usefull and ported it to VFP.

The function returns the number of working days between two dates.  It excludes Holidays and weekends.   The Holidays portion is defined by 2 arrays.  Both arrays are internal to the function itself.  1 Array is holidays that never change.  Like Xmas (12/25/XXXX) The 2nd array is floating holidays that change.  Like labor day (1rst Monday in September).  Array records can be added to include additonal holidays.  The function has notes that explain how to do this.  Anyhow... Enjoy!

*Example
dStart = {^1999-10-10}
dEnd = {^2000-10-10}
? WorkingDays(dStart, dEnd)

Function WorkingDays(dStart, dEnd)
    *Check for year Span (Determines size of holiday array)
    nSpan = YEAR(dEnd)-YEAR(dStart)
    If nSpan < 0 THEN &&Idiot programmer... FLIP'M
      dTemp = dEnd
      dEnd = dStart
      dStart = dTemp
      nSpan = YEAR(dEnd)-YEAR(dStart)
    ENDIF
    
    nCounter = 0
    nStandardHolidays = 3 &&Holidays dates that do not change
    Dimension gSHolidays(nStandardHolidays)
    gSHolidays(1) = {^2000-01-01} &&New Year
    gSHolidays(2) = {^2000-07-04} &&Independence
    gSHolidays(3) = {^2000-12-25} &&Xmas

    nFloatingHolidays = 3 &&Holidays dates that change
    Dimension gFHolidays(nFloatingHolidays,4)
    *Labor Day
    gFHolidays(1,1) = 9 &&Month
    gFHolidays(1,2) = 2 &&Day of the week Sunday = 1
    gFHolidays(1,3) = 1 &&Occurence  IE (11,5,4,0) = 4th Thursday in Novemeber
    gFHolidays(1,4) = 0 &&Offset plus this many days
    *Thanksgiving
    gFHolidays(2,1) = 11
    gFHolidays(2,2) = 5
    gFHolidays(2,3) = 4
    gFHolidays(2,4) = 0     
    *Day After Thanksgiving
    gFHolidays(3,1) = 11
    gFHolidays(3,2) = 5
    gFHolidays(3,3) = 4
    gFHolidays(3,4) = 1 &&IE (11,5,4,1) = Friday after the 4th Thursday in Novemeber

    nHolidayCount = (nFloatingHolidays + nStandardHolidays)*(nSpan +1)    
    DIMENSION gHolidays(nHolidayCount)
    nHC = 1
    DO WHILE nCounter <= nSpan
      nYear = YEAR(dStart)+nCounter
      *Add Standard Holidays
      FOR x = 1 TO ALEN(gSHolidays)
        nMonth = MONTH(gSHolidays(x))
        nDay = DAY(gSHolidays(x))
        cDate = "{^"+ALLTRIM(STR(nYear))+"-"+ALLTRIM(STR(nMonth))+"-"+ALLTRIM(STR(nDay))+"}"
        gHolidays(nHC)= &cDate
        nHC = nHC + 1
      NEXT
      *Add Floating Holidays
      FOR x = 1 TO ALEN(gFHolidays)/4
        nMonth = gFHolidays(x,1)
        nFindDay = gFHolidays(x,2)
        nOccur = gFHolidays(x,3)
        nOffset = gFHolidays(x,4)
        cDate = "{^"+ALLTRIM(STR(nYear))+"-"+ALLTRIM(STR(nMonth))+"-01}"
        dTemp = &cDate &&1rst day of Holiday's Month
        nCheckM = nMonth
        nCheckD = 1
        DO WHILE nCheckM = nMonth &&Scan the month for nOccurence of the Day
          IF DOW(dTemp) = nFindDay THEN
            IF nCheckD = nOccur THEN
              EXIT
            ELSE
              nCheckD = nCheckD + 1
            ENDIF
          ENDIF
          dTemp = dTemp +1
          nCheckM=MONTH(dTemp)
        ENDDO
        dTemp = dTemp + nOffset
        gHolidays(nHC)= dTemp
        nHC = nHC + 1
      NEXT
      nCounter = nCounter +1
    ENDDO
    RELEASE gFHolidays, gSHolidays
    =ASORT(gHolidays) &ΒΌ really needed
    
    *Count work days between dates
    nCalenderDays = dEnd - dStart
    nCountHolidays = 0
    nWorkDays = 0
    dTemp = dStart
    FOR x = 1 TO nCalenderDays
      nDOW = DOW(dTemp)
      nHoliday = ASCAN(gHolidays,dTemp)
      IF (nDOW = 1 OR nDOW = 7)    THEN  
        nCountHolidays = nCountHolidays+1
      ENDIF
      IF nHoliday > 0 THEN &&Counts holidays that occur on weekends
        nCountHolidays = nCountHolidays+1
      ENDIF
      dTemp = dTemp + 1
    NEXT
    nWorkDays = nCalenderDays - nCountHolidays
    RETURN nWorkDays
ENDFUNC    

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close