Number of business days between two dates
ProAdmin assumes business days are Mondays thru Fridays, where holidays (e.g., Federal Holidays) are considered regular business days. The date operator #DAYOFWEEK indicates the day of week: 1 = Monday, 2= Tuesday, 3 = Wednesday, 4= Thursday, 5 = Friday, 6 = Saturday, and 7 = Sunday. First is the expression without detailed comments, and is the expression with a detailed explanation.
Note, that this expression will include a business day for both the start and stop dates (if they are business days).
This is the expression without detailed comments:
DOW1:= #DAYOFWEEK DT1_DT &
DOW2:= #DAYOFWEEK DT2_DT &
SUN1_DT:= DT1_DT - DOW1 & ; the Sunday before DT1
SUN2_DT:= DT2_DT - DOW2 & ; the Sunday before DT2
BusDays1:= 5 * ((SUN2_DT - SUN1_DT)/7) & ; convert weeks into business days
BusDays:= BusDays1 + (5 #MIN DOW2)- (5 #MIN (DOW1 - 1)) &
This is the expression with detailed comments:
; M T W T F S S Day of week
; 1 2 3 4 5 6 7 #DAYOFWEEK
DOW1:= #DAYOFWEEK DT1_DT & ; earlier date
DOW2:= #DAYOFWEEK DT2_DT & ; dates must be greater than or equal to DT1
SUN1_DT:= DT1_DT - DOW1 & ; the Sunday before DT1
SUN2_DT:= DT2_DT - DOW2 & ; the Sunday before DT2
; #DAYOFWEEK happens to return the number of days since the previous
; Sunday. For example, Monday - 1 day is Sunday, Tuesday - 2 days
; is Sunday, Wednesday - 3 days is Sunday, .... Subtracting the day
; of the week from it’s date always gives us the previous Sunday.
BusDays1:= 5 * ((SUN2_DT - SUN1_DT)/7) & ; convert weeks into business days
; SUN2-SUN1 is the number of days between SUNDAYS; it’s always a
; multiple of 7 (SUN2=SUN1, SUN2<sun1,>SUN1) and
; (SUN2-SUN1)/7 is the number of completed weeks between the 2
; Sundays. Multiplying by 5 give us the number of business days
; between the 2 Sundays.</sun1,>
BusDays:= BusDays1 + (5 #MIN DOW2) - (5 #MIN (DOW1 - 1)) &
; Since we are working with business days, we need to limit the
; day of the week to Monday-Friday (e.g., 5 #MIN). Why are we
; subtracting 1 day from DOW1? Suppose DT1 and DT2 are the same
; date, Wednesday 7/8/2009. Then DOW2-DOW1 is 0 (=3-3), but we want
; 1 day worked. So, we need subtract one day from DOW1 (as in the
; current expression), or we need to add one day to DOW2 and adjust
; the expression to (6 #MIN (1+DOW2)) – (5 #MIN DOW1). We’d have
; to explain why we’re using 6 (instead of 5): 5 #MIN DOW2 is
; equivalent to (5+1) #MIN (DOW2+1).