Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Need to find first monday..friday of month

Hi

I need to find a specific day in a month based on an occurance of that day.

ie I need to find first monday of month, second tuesday of month, 4th friday of month .. etc Basically any day in a cycle of a month over a period of a year.

Thanks
Chuck
0
Charles Baldo
Asked:
Charles Baldo
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about something like?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Find the second Monday in October of 2006...
        Dim dt As DateTime = GetSpecificDate(2, DayOfWeek.Monday, 10, 2006)
        Debug.WriteLine(dt.ToString)
    End Sub

    Private Function GetSpecificDate(ByVal occurence As Integer, ByVal dayOfWeek As DayOfWeek, ByVal month As Integer, ByVal year As Integer) As DateTime
        Dim returnDt As DateTime = Nothing

        Try
            Dim counter As Integer = 0
            Dim startDt As DateTime = New DateTime(year, month, 1, 0, 0, 0)
            If occurence >= 1 Then
                While True
                    While startDt.DayOfWeek <> dayOfWeek
                        startDt = startDt.AddDays(1)
                    End While
                    counter = counter + 1

                    If counter = occurence Then
                        If startDt.Month = month Then
                            returnDt = startDt
                        End If

                        Exit While
                    Else
                        startDt = startDt.AddDays(1)

                        If startDt.Month <> month Then
                            Exit While
                        End If
                    End If
                End While
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Finding Date", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        Return returnDt
    End Function
0
 
Charles BaldoSoftware DeveloperAuthor Commented:
Looks workable

Thanks a ton Idle Mind
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now