Solved

Need to find first monday..friday of month

Posted on 2006-11-01
2
437 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:Charles Baldo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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:Charles Baldo
ID: 17854568
Looks workable

Thanks a ton Idle Mind
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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