# 109. Count Weekdays or Weekends

# Description

Determine the number of weekdays or weekends that fall within a given range of dates.

# Code

Count Weekend Days:

// Clear temporary variable

SET Temp-n TO 0

// Odd days

REMAINDER( DAYS FROM( DateA-d, DateB-d ), 7 )

IF RESULT > 0

SET Temp-n TO DAY OF WEEK( DateA-d ) + RESULT

IF Temp-n >= 8

IF RESULT > 1 AND DAY OF WEEK( DateA-d ) < 7

SET Temp-n TO 2

ELSE

SET Temp-n TO 1

END IF

ELSE IF Temp-n = 7

SET Temp-n TO 1

ELSE

SET Temp-n TO 0

END IF

END IF

// Plus two weekend days per week

DAYS FROM( DateA-d, DateB-d ) / 7

TRUNCATE( RESULT, 0 )

RESULT * 2

// Include the start date if it is a weekend day

IF DAY OF WEEK( DateA-d ) = 1 OR DAY OF WEEK( DateA-d ) = 7

RESULT + 1

END IF

// Return a value

RESULT + Temp-n

Explanation

Required variables:

• DateA-d - Date variable containing the start date for the range

• DateB-d - Date variable containing the end date for the range

• Temp-n - A temporary number variable

This computation calculates the number of weekends that fall within a given range of dates. Note that both the start date and the end date are included in the count.

To make the count, we first look at how many "odd" days there are. Odd days are days short of one week. The odd days are found by dividing the number of days between DateA-d and DateB-d by seven, and taking the REMAINDER. Next we add this number to the DAY OF WEEK value. This gives us a span of days that may or may not include 7 (Saturday) and 8 (Sunday). For example, if the odd dates start on Friday (DAY OF WEEK = 6) and are four days long (6 + 4 = 10), we see that weekdays 7 and 8 (Saturday and Sunday) both fall within the range. So we count two weekend days. The computation also checks for a couple of exceptions which are difficult to explain. See if you can figure them out for yourself some afternoon.

We store our total at this point in the temporary number variable Temp-n.

The remainder of the computation is more straightforward. We can now figure the number of complete weeks between DateA-d and DateB-d by determining the DAYS FROM DateA-d to DateB-d and dividing by 7. Using TRUNCATE, we trim off any decimal value. We now multiply this number of weeks by two to find the number of weekend days.

Finally, we should include DateA-d in this count if it falls on a weekend.

The computation then returns the total of all weekend dates found in the range as a number value.

Code

Count Weekdays (Business Days):

// Clear temporary variable

SET Temp-n TO 0

// Odd days

REMAINDER( DAYS FROM( DateA-d, DateB-d ), 7 )

IF RESULT > 0

SET Temp-n TO DAY OF WEEK( DateA-d ) + RESULT

IF Temp-n >= 8

IF RESULT > 1 AND DAY OF WEEK( DateA-d ) < 7

RESULT - 2

ELSE

RESULT - 1

END IF

ELSE IF Temp-n = 7

RESULT - 1

END IF

END IF

SET Temp-n TO RESULT

// Plus five weekdays per week

DAYS FROM( DateA-d, DateB-d ) / 7

TRUNCATE( RESULT, 0 )

RESULT * 5

// Include the start date if it is a weekday

IF DAY OF WEEK( DateA-d ) > 1 AND DAY OF WEEK( DateA-d ) < 7

RESULT + 1

END IF

// Return a value

RESULT + Temp-n

Explanation

This computation is substantially the same as the first, with the exception that we assume all odd days are weekdays and then subtract any weekend days we find. Then we add five days for each complete week found in the range. And we add one more day if DateA-d happens to fall on a weekday.

# Explanation