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

Determine which Friday of the month? 1

Status
Not open for further replies.

fugly

Technical User
Mar 21, 2002
29
US
I have a situation where code is supposed to run on the third friday of the month? How do you program that?
 
Call a function something like this passing in the current date


Public Function fn_IsDateThirdFridayOfMonth(iDate As Date) As Integer
Dim iDaysTofri As Integer
Dim dtTempDate As Date


'get the third friday in the month
dtTempDate = CDate("01" & "/" & DatePart("m", iDate) & "/" & DatePart("yyyy", iDate))
iDaysTofri = 7 - DatePart("w", dtTempDate)
dtTempDate = DateAdd("d", 21 + iDaysTofri - 1, dtTempDate)
If dtTempDate = iDate Then fn_IsDateThirdFridayOfMonth = 1 Else fn_IsDateThirdFridayOfMonth = 0

End Function
 

Nope, that won't always work.

We have had some threads in the past where similar questions were asked, and should only need a slight modification.
 
A minor variation on the theme:

Code:
Public Function dtNthDOW(dtDtIn As Date, intNthWkDay, Optional intDOW As Integer = vbFriday) As Date

    'Michael Red    11/8/2003   Return the Date of the Nth Day of the Week _
     in the Month of the input date.  Minimal error checking, Returns Sone _
     date in the year of 1899 if the Nth Day of the Week does not fall in _
     the same month as the date in.

    'Example of a normal return
    '? dtNthDOW(DAte, 3)
    '11/21/03

    'Example of an error return
    '? dtNthDOW(DAte, 6, vbsunday)
    '12/29/1899


    Dim dtFstOfMnth As Date

    dtFstOfMnth = DateSerial(Year(dtDtIn), Month(dtDtIn), 1)
    While Weekday(dtFstOfMnth) <> intDOW
        dtFstOfMnth = dtFstOfMnth + 1
    Wend

    dtNthDOW = DateAdd(&quot;d&quot;, (intNthWkDay - 1) * 7, dtFstOfMnth)
    If (Month(dtDtIn) <> Month(dtNthDOW)) Then
        dtNthDOW = -1
    End If

End Function





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
If haven't tested this completely, but it might work:
Code:
Function ThirdFriday(ByVal InputDate As Date) As Date
    InputDate = DateSerial(Year(InputDate), Month(InputDate), 1)
    ThirdFriday = InputDate + 21 - Weekday(InputDate, vbSaturday)
End Function
Call it like:
If Date() = ThirdFriday(Date) Then
'Run Task
End If

If you want to use this for another day, then just change the vbSaturday to the day After the weekday you are looking for, and where I used 21 for the third week, use 7 for the first week, 14 for the secnd, etc.
 
I tried them all and Michael's worked the best for me!!! Thank you to everyone though. You guys are great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top