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

Date Problem

Status
Not open for further replies.

EAS

Programmer
May 21, 2001
25
GB
How do I get the 4th Tuesday of every month?
 
you're doing this in CF, right?

start a loop with index=1 for january

look at the 1st of the month, and see if it's a tuesday

if it isn't, go on the the 2nd, then the 3rd, and eventually you will find a tuesday

then add 28 days, voila, the 4th tuesday of that month

repeat for the next month -- note that february always has 4 tuesdays, even if it's only 28 days long

loop through all the months, and you're done


make sense?


rudy
 
Brilliant - Thanks Rudy, I'm implementing it now
Liz
 
thanks, liz

it was indeed a most interesting problem, so i created a page which displays The Nth Weekday of the Month --


note that it handles the 5th correctly

here is the guts of the code --

[tt]<!--- loop through all 12 months --->
<CFLOOP INDEX=&quot;m&quot; FROM=&quot;1&quot; TO=&quot;12&quot;>
<!--- thedate starts as the 1st of the month --->
<CFSET thedate = CreateDate(myyear,m,1)>
<!--- loop over current and next six days --->
<CFLOOP INDEX=&quot;wd&quot; FROM=&quot;0&quot; TO=&quot;6&quot;>
<CFIF DayOfWeek(thedate) IS myweekday>
<CFSET dayofmonth=Day(thedate)>
<CFBREAK>
</CFIF>
<CFSET thedate=DateAdd(&quot;d&quot;,1,thedate)>
</CFLOOP>
<!--- move to the nth of the month --->
<CFSET n = Left(mynth,1) * 7 - 7>
<CFSET nthdate = CreateDate(myyear,m,dayofmonth)>
<CFSET nthdate = DateAdd(&quot;d&quot;,n,nthdate)>
<!--- if nth was 5th, it is possible that
we have gone past the end of the month --->
<CFIF Month(nthdate) EQ m>
<CFOUTPUT>
<br />#DateFormat(nthdate,&quot;mmm dd&quot;)#
</CFOUTPUT>
<CFELSE>
<CFOUTPUT>
<br />#DateFormat(thedate,&quot;mmm&quot;)# has none
</CFOUTPUT>
</CFIF>
</CFLOOP>[/tt]

contact me via if you want the rest of the source including the form fields and error checking


rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top