Find date of 2nd Thursday of each month

T Hoecherl
T Hoecherl used Ask the Experts™
on
I need to calculate a date at the beginning of each month which represents the 2nd Thursday of that month.  How can I do that?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Here's a quick example:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim thursday As DateTime = SecondThursday(4, 2017)
        Debug.Print(thursday)
    End Sub

    Private Function SecondThursday(ByVal month As Integer, ByVal year As Integer) As DateTime
        Dim dt As New DateTime(year, month, 1)
        ' find the first Thursday
        While dt.DayOfWeek <> DayOfWeek.Thursday
            dt = dt.AddDays(1)
        End While
        dt = dt.AddDays(7) ' jump to the second Thursday
        Return dt
    End Function

Open in new window

Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi thoecherl;

This Linq query should give you what you need.
Dim today As DateTime = DateTime.Today
' Find the second thursday of the month
Dim secondThursday As DateTime = Enumerable.Range(8, 7) _
	.Select(Function(dayIdx) New DateTime(today.Year, today.Month, dayIdx)) _
	.Where(Function(theDate) theDate.DayOfWeek = DayOfWeek.Thursday) _
	.Single()

Open in new window

T HoecherlDeveloper

Author

Commented:
Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial