Solved

Need to find first monday..friday of month

Posted on 2006-11-01
2
404 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

13 Experts available now in Live!

Get 1:1 Help Now