> Step 2 - create a while (aka conditional) loop. Inside
this loop, add a day
> to your date variable until the day of the week is the
one you want.
But you don't need to loop around to find it, you already
know it.
If the first day of the month is a Tues (DOW:3), then
Thursday is going to
be offset from that by 2, because Thurs is DOW:5. You don't
need to loop
through Tues and Weds to check if they are Thurs: they're
not. you already
know this.
So the process is:
What's the DOW of July 1?
dayOfWeek(createDate(2008,7,1)) = 3 (Tue)
If you want Thurs, then it's the 5th day of the week, which
is 5 - 3 = 2
days offset from the date of the 1st of the month.
And the second Thurs is +7 days from that (ie: a week later).
Please see my code and result...not working for date passed.
------------------------------------------------------------------------------------------------------------------------
Result
DOWFirstofMonth : 1
AptDate : June 12, 2008 -- Correct for this month
Compresult : 1
Nextmonth : {ts '2008-07-13 00:00:00'}
NextmonthDayOfWeek : 3
AptDate : July 14, 2008 -- not correct (Result is suppose to
be July 10, 2008)
--------------------------------------------------------------------------------
Find the DOW of the first of the month.
Determine the offset between that and the DOW that which you
want to find.
Add that to the date of the 1st of month.
Add another week.
Read the docs for:
createDate()
dayOfWeek()
dateAdd()