83. First Wednesday of the Month
Description
How to determine what day of the month a fixed weekday will fall on (e.g. the first Wed. of the month).
Code
// Find the first day of next month
DATE OF(
1 ,
MONTH OF( TODAY + 1 MONTH ) ,
YEAR OF( TODAY + 1 MONTH )
)
// Sunday - Tuesday
IF DAY OF WEEK( RESULT ) < 4
RESULT + ( 4 - DAY OF WEEK( RESULT ) ) DAYS
// Thursday - Saturday
ELSE IF DAY OF WEEK( RESULT ) > 4
RESULT + ( 7 - DAY OF WEEK( RESULT ) + 4 ) DAYS
END IF
Explanation
The example above will determine what day the first Wednesday of next month falls on. It begins by finding the first day of next month, which will be used as a reference point. This date is stored in RESULT. We can look at what day of the week the first day of the month falls on through the DAY OF WEEK model. This model returns 1 for Sunday, 2 for Monday ... and 7 for Saturday.
If the first day of the month is before Wednesday (less than 4, meaning between 1 and 3), we advance the date to Wednesday by adding 4 - DAY OF WEEK days. For example, if the first day of the month lands on Monday, or DAY OF WEEK = 2, we can advance to Wednesday by adding 4 - 2 = 2 days. Monday + 2 days = Wednesday.
If the first day of the month is after Wednesday (greater than 4, meaning between 5 and 7), we advance the date to Wednesday by adding 7 - DAY OF WEEK + 4 days. For example, if the first day of the month lands on Thursday, or DAY OF WEEK = 5, we can advance to Wednesday by adding 7 - 5 + 4 = 6 days. Thursday + 6 days = Wednesday.
You can adapt this computation by changing the "4," which here represents Wednesday, to a different day of the week, such as "6" for Friday or "2" for Monday. You can also get a different week by adding 7 days for each additional week. For example, to get the second Wednesday of the month rather than the first, add this line to the end of the computation:
RESULT + 7 DAYS