Example shown: How to find the 2nd Wednesday of the month.
<cfset this_month = #DateFormat(now(),'mm')#&"/01/"&#dateFormat(now(),'yyyy')#>;
<cfset MonthArray = ArrayNew(1)>;
<cfloop from="1" to="14" index="loopCount">;
<blockquote>
<cfset MonthArray[1][#loopCount#] = #DateFormat(this_month,'dddd')#>;
<cfif MonthArray[1][#loopCount#] is 'Wednesday'>;
<blockquote>
<cfset maxwed = #loopCount#>;
</blockquote>
</cfif>;
</blockquote>
<Cfset this_month = #dateAdd('d',1,this_month)#>;
</cfloop>;
<cfoutput>The result of the above query when run today would be that in #DateFormat(now(),'mmmm yyyy')# the 2nd Wednesday is on the #maxwed#th.
So here is how it works, I start by creating a variable that takes the current month and year in number format, and put it in a string that creates a mm/dd/yyyy value but forcing it be the first of the month.
Then we make a new array, in this case we are only going to need a 1 dimensional array.
Next is the loop, now for example, if you wanted to know what the first Monday of the month is you would set the loop to be from 1 to 7. Logic being that there are only seven days in a week and if you want the first instance of the value "Monday" you only need to loop for 1 week. The loop creates a value that is the text version of day of the week, then an IF statement stores the numerical value if the value is in this example, 'Wednesday'. Now since I want the LAST Wednesday, I will let the value get over written, this way when I call the variable, its the last successful iteration of the if statement being true.
The last obvious question is, why create the array? Yes, you could do this without the array, however I like to build so that I can make additions quickly.
One of the sites that I worked with was an Aero Club and we used a slightly different version of the script above. In this case on the meeting announcement page we need to let the membership know when the next board meetings will be. However if the meeting for this month has already passed, then we need the dates to adjust. Also we need to not only show what this months meeting date is, but next months. Here is how this is done.
<cfset this_month = DateFormat(now(),'mm')&"/01/"&dateFormat(now(),'yyyy')>;
<cfset next_month = DateAdd('m',1,this_month)>;
<cfset third_month = DateAdd('m',2,this_month)>;
<cfset MonthArray = ArrayNew(1)>;
<cfloop from="1" to="14" index="loopCount">;
<blockquote>
<cfset MonthArray[1][loopCount] = DateFormat(this_month,'dddd')>;
<cfset MonthArray[2][loopCount] = DateFormat(next_month,'dddd')>;
<cfset MonthArray[3][loopCount] = DateFormat(third_month,'dddd')>;
<blockquote>
<cfif MonthArray[1][loopCount] is 'Wednesday'>;
<blockquote><cfset max_this_wed = DateFormat(now(),'mm')&"/"&LoopCount&"/"&dateFormat(now(),'yyyy')>;</blockquote>
</cfif>;
<cfif MonthArray[2][loopCount] is 'Wednesday'>;
<blockquote><cfset max_next_wed = DateFormat(now(),'mm')+1&"/"&LoopCount&"/"&dateFormat(now(),'yyyy')>;</blockquote>
</cfif>;
<cfif MonthArray[3][loopCount] is 'Wednesday'>;
<blockquote><cfset max_third_wed = DateFormat(now(),'mm')+2&"/"&LoopCount&"/"&dateFormat(now(),'yyyy')>;</blockquote>
</cfif>;
</blockquote>
<Cfset this_month = dateAdd('d',1,this_month)>;
<Cfset next_month = dateAdd('d',1,next_month)>;
<Cfset third_month = dateAdd('d',1,third_month)>;
</blockquote>
</cfloop>;
<cfif this_month MOD 2>;
<blockquote>
<cfif this_month lt Now()>;
<blockquote>
<Cfset even = max_third_wed>;
<cfelse>;
<Cfset even = max_this_wed>;
</blockquote>
</cfif>;
<cfset odd = max_next_wed>;
<cfelse>;
<Cfset even = max_next_wed>;
<cfif this_month lt Now()>;
<blockquote>
<Cfset ODD = max_third_wed>;
<cfelse>;
<Cfset ODD = max_this_wed>;
</blockquote>
</cfif>;
</blockquote>
</cfif>;
You can see that basically we are doing the same function as in the original example, but this time we are adding two more months to the mix. The last IF statement is what is used to tell if today is greater than the meeting date for this month, and if it is, then jump the next month for that particular meeting type.