NthWeekdayDate Function:
Find the date of the specified day within the month.
Description:
NthWeekdayDate Function finds the date of the specified day within the month. For example, retrieve the 3rd Sunday's date from the actual date.
Syntax:
Date = NthWeekdayDate(dtDate, intN, intDayOfWeek)
Details:
Arguments:
dtDate: Starting date for the search. If this isn't the first day of the month, the code moves back to the first.
intN: Number of the specific day, within the month. If larger than there are days of the specified type in the month, return the date of the requested day anyway. If you ask for the 10th Monday, the code will just find the first Monday in the specified month, and then add 10 weeks to that date.
intDayOfWeek: the day of the week to seek 1 = Sunday 2 = Monday 3 = Tuesday 4 = Thursday 5 = Wednesday 6 = Friday 7 = Saturday
Example:
<%
Response.write NthWeekdayDate("22/12/2006", 3, 1) '--- returns: 17/12/2006 (the date of the 3rd sunday of december 2006)
%>
ASP Source Code:
<%
Function NthWeekdayDate(dtmDate, intN, intDayOfWeek) '--- Find the date of the specified day within the month. '--- For example, retrieve the 3rd Sunday's date. Dim dtmTemp, boolErr On Error Resume next If (intDayOfWeek < vbSunday Or intDayOfWeek > vbSaturday) Or (intN < 1) Then '--- Invalid parameter values. Just return the passed-in date. NthWeekdayDate = dtmDate Exit Function End If '--- Get the first day of the month. dtmTemp = DateSerial(Year(dtmDate), Month(dtmDate), 1) '--- Get to the first intDayOfWeek in the month. Do While WeekDay(dtmTemp) <> intDayOfWeek dtmTemp = dtmTemp + 1 Loop '--- Now you've found the first intDayOfWeek in the month. '--- Just add 7 for each intN after that. NthWeekdayDate = dtmTemp + ((intN - 1) * 7) if Err Then boolErr = True end If On Error goto 0 if boolErr then Err.Raise 5104, "NthWeekdayDate Function", Err.Description End Function
%>
|