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

How to count working days

Usefull Functions & Procedures

How to count working days

by  BlindPete  Posted    (Edited  )
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top