determine the nth monday of the month

# Thread: determine the nth monday of the month

1. Senior Member
Join Date
Dec 1969
Posts
413

## determine the nth monday of the month

How can I determine the nth monday of the month?<BR>I have been messing with datePart,dateDiff, dateSerial, etc... but cant figure out how to piece them together to get them to return the nth monday of the month.<BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## Messy But works

Function nthmonday(Mon, Years, factors)<BR> firstdayofmonth = Datepart("w", Dateserial( Years, Mon, "1"))<BR> if firstdayofmonth = VBMonday then<BR> FirstofMonth = Dateserial( Years, Mon, "1")<BR> elseif firstdayofmonth &#062; datep then<BR> FirstofMonth = Dateserial( Years, Mon, 8 - (firstdayofmonth - VBMonday) )<BR> else<BR> FirstofMonth = Dateserial( Years, Mon, 1 + (firstdayofmonth - VBMonday) )<BR> end if<BR> if factors = 1 then<BR> nthmonday = firstofmonth<BR> else<BR> nthmonday = dateadd( "d", 7 * (Factors -1), firstofMonth)<BR> end if<BR>end Function<BR><BR>test = nthMonday(9, 03, 2)<BR>response.write "<BR>" & formatdatetime(test, vblongdate)

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## *must* be a short way...

let&#039;s play with it ...<BR><BR><BR>**************<BR><BR><BR>&#060;HTM L&#062;&#060;BODY&#062;<BR>&#060;%<BR>Function WhichMonday( yr, mn, which )<BR> firstOfMonth = DateSerial( yr, mn, 1 )<BR> firstMonday = firstOfMonth - Weekday(firstOfMonth,vbMonday) + 1<BR> If Month(firstMonday) = mn Then which = which - 1<BR> WhichMonday = firstMonday + 7 * which<BR>End Function<BR>%&#062;<BR><BR>Some tests:&#060;UL&#062;<BR><BR>&#060;LI&#062;1st Monday of Sept 2003: &#060;%= FormatDateTime( whichMonday(2003,9,1) ) %&#062;<BR><BR>&#060;LI&#062;2nd Monday of Sept 2003: &#060;%= FormatDateTime( whichMonday(2003,9,2) ) %&#062;<BR><BR>&#060;LI&#062;3rd Monday of Sept 2003: &#060;%= FormatDateTime( whichMonday(2003,9,3) ) %&#062;<BR><BR>&#060;LI&#062;4rd Monday of Sept 2003: &#060;%= FormatDateTime( whichMonday(2003,9,4) ) %&#062;<BR><BR>&#060;LI&#062;4rd Monday of Oct 2003: &#060;%= FormatDateTime( whichMonday(2003,10,4) ) %&#062;<BR><BR>&#060;LI&#062;4rd Monday of Nov 2003: &#060;%= FormatDateTime( whichMonday(2003,11,4) ) %&#062;<BR><BR>&#060;LI&#062;4rd Monday of Dec 2003: &#060;%= FormatDateTime( whichMonday(2003,12,4) ) %&#062;<BR><BR>&#060;LI&#062;4rd Monday of Jan 2004: &#060;%= FormatDateTime( whichMonday(2004,1,4) ) %&#062;<BR><BR>&#060;/UL&#062;&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR><BR>***********<BR><BR>Yep, works. Check into the ability to use the second argument of the WEEKDAY function for fun and profit.<BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## A misnamed variable...

"firstMonday" isn&#039;t, really. Since it might be the last Monday in the prior month.<BR><BR>"priorMonday" might be a better name, except that if the the first of the month happens to be Monday than "priorMonday" and "firstOfMonth" are the same.<BR><BR>Well, you rename it to suit yourself.<BR><BR>Call it FRED if you like. That&#039;s our cat&#039;s name.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Omitted vbLongDate...

...from all the calls to FormatDateTime. Sorry. Much clearer with that in there. (Doesn&#039;t change answer, of course.)<BR><BR>Example:<BR><BR>&#060;LI&#062;1st Monday of Sept 2003: &#060;%= FormatDateTime( whichMonday(2003,9,1), vbLongDate ) %&#062;<BR><BR>The output is now easier to understand:<BR><BR>1st Monday of Sept 2003: Monday, September 01, 2003 <BR>2nd Monday of Sept 2003: Monday, September 08, 2003 <BR>3rd Monday of Sept 2003: Monday, September 15, 2003 <BR>4rd Monday of Sept 2003: Monday, September 22, 2003 <BR>4rd Monday of Oct 2003: Monday, October 27, 2003 <BR>4rd Monday of Nov 2003: Monday, November 24, 2003 <BR>4rd Monday of Dec 2003: Monday, December 22, 2003 <BR>4rd Monday of Jan 2004: Monday, January 26, 2004 <BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•