Solved

Need to find first monday..friday of month

Posted on 2006-11-01
2
413 Views
Last Modified: 2010-04-23
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
Comment
Question by:charlesbaldo
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17854434
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
 

Author Comment

by:charlesbaldo
ID: 17854568
Looks workable

Thanks a ton Idle Mind
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now