Solved

get the first date of a particular day for every month

Posted on 2011-02-11
4
304 Views
Last Modified: 2012-05-11
I have a date , based on that date I need to find the first date of that particualr day for a date range

So if I have a start date and end date. And my date is say feb 7th, which is a monday, then I need to find all the first mondays for every month between the start and end date.
Does not matter if it is a holiday.
So my next day will be Mrch 8th, Apr 4th and son unitl I reach the end date boundary
0
Comment
Question by:countrymeister
[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
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34872268
Something like this?

Logic
Public Function GetFirstDays(ByVal startDate As DateTime, ByVal endDate As DateTime) As List(Of DateTime)
	Dim result As New List(Of DateTime)
	Dim day As DayOfWeek = startDate.DayOfWeek
	Dim temp As New DateTime(startDate.Year, startDate.Month, 1)

	temp = temp.AddMonths(1)

	While temp < endDate
		While temp.DayOfWeek <> day
			temp = temp.AddDays(1)
		End While

		If temp < endDate Then result.Add(temp)

		temp = New DateTime(temp.Year, temp.Month, 1)
		temp = temp.AddMonths(1)
	End While

	Return result
End Function

Open in new window


Usage
Sub Main()
    Dim start As New DateTime(2011, 2, 7)
    Dim halt As New DateTime(2011, 5, 1)
    Dim days As List(Of DateTime) = GetFirstDays(start, halt)
End Sub

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34872295
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34872649
*This is very similar to kaufmed's answer...it seemed to me that the reference date might not actually be the start date (or even within the range):
Private Function FirstDays(ByVal startDT As DateTime, ByVal stopDT As DateTime, ByVal refDT As DateTime) As List(Of DateTime)
        Dim results As New List(Of DateTime)
        Dim targetDay As DayOfWeek = refDT.DayOfWeek

        Dim tmpDT As DateTime
        Dim dt As New DateTime(startDT.Year, startDT.Month, 1)
        While dt <= stopDT
            tmpDT = dt
            While tmpDT.DayOfWeek <> targetDay
                tmpDT = tmpDT.AddDays(1)
            End While
            If tmpDT >= startDT AndAlso tmpDT <= stopDT Then
                results.Add(tmpDT)
            End If
            dt = dt.AddMonths(1)
        End While

        Return results
    End Function

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 34873431
Hi countrymeister;

If I understand the question correctly this should do what you want.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim dts As DateTime = New DateTime(2011, 11, 12)
    Dim dte As DateTime = New DateTime(2012, 2, 20)
    Dim dates As List(Of DateTime)
    dates = GetMonthStarts(dts, dte)

End Sub

Private Function GetMonthStarts(ByVal startDate As DateTime, ByVal endDate As DateTime) As List(Of DateTime)

    ' Used to return the list of DateTime
    Dim dates As New List(Of DateTime)
    ' Get the day of the week to use as the first day of the month
    Dim startDay As Integer = startDate.DayOfWeek
    ' Used to keep track of whivh month we are working on
    Dim processDate As DateTime = startDate.AddMonths(1)
    ' Starting with month
    Dim startMonth As Integer = startDate.Month
    ' Total number of months in the range
    Dim totalMonths As Long = DateDiff(DateInterval.Month, startDate, endDate)

    ' Add the start date to the list to be returned
    dates.Add(startDate)

    For idx As Integer = 0 To totalMonths - 2
        ' Used to see which day of the begining of the month we want to get
        Dim calcDate As DateTime = New DateTime(processDate.Year, processDate.Month, 1)

        ' See if the calcDate is the one we are looking for
        If calcDate.DayOfWeek = startDay Then
            ' Add to the list of returned dates
            dates.Add(calcDate)
            ' get ready for the next month
            processDate = processDate.AddMonths(1)
        Else
            ' Find the day in the begining of the month we are looking for.
            Do
                If calcDate.DayOfWeek = startDay Then
                    dates.Add(calcDate)
                    processDate = processDate.AddMonths(1)
                    Exit Do
                End If
                calcDate = calcDate.AddDays(1)
            Loop While calcDate.DayOfWeek <= startDay
        End If
    Next

    Return dates

End Function

Open in new window


Fernando
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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