Solved

VB.NET Week Day Dates

Posted on 2013-01-26
3
1,417 Views
Last Modified: 2013-01-27
Hello,

I am trying to get an array of dates for a given month.

For example, the user enters the month they want e.g. September 2012.

I then need to find the first and last date of that month. (Done that)

Then I need to get the dates for each week in that month.

So it will be formatted like the below.

3 September 2012 - 7 September 2012
10 September 2012 - 14 September 2012
17 September 2012 - 21 September 2012
21 September 2012 - 28 September 2012

I need the first date it gets to start on the first week day for that month so ignoring Saturday and Sunday.
I need it to finish on the very last date of that month but during the week.

Another example would be for August 2012

1 August 2012 - 3 August 2012
6 August 2012 - 10 August 2012
13 August 2012 - 17 August 2012
20 August 2012 - 24 August 2012
27 August 2012 - 31 August 2012


I have currently got a loop which is like the below.

   Do While blStop = False
            ' TextBox1.Text &= dtestart.ToLongDateString & vbNewLine
            If dtestart.Month <> dtestart.AddDays(x).Month Then
                '    MsgBox("Month has changed")
                arrDates(TheCounter) = dtestart & " - " & dtestart.AddDays(x - 1)
                TheCounter += 1
                ReDim Preserve arrDates(TheCounter)
                blStop = True
            Else
                If dtestart.AddDays(x).DayOfWeek = DayOfWeek.Friday Then
                    arrDates(TheCounter) = dtestart & " - " & dtestart.AddDays(x)
                    TheCounter += 1
                    ReDim Preserve arrDates(TheCounter)
                    dtestart = dtestart.AddDays(x + 3)
                    x = 0
                ElseIf dtestart.AddDays(x).DayOfWeek = DayOfWeek.Monday Then
                    ' dtestart = dtestart.AddDays(x)
                    x = 0
                ElseIf dtestart.AddDays(x).DayOfWeek = DayOfWeek.Saturday Or dtestart.AddDays(x).DayOfWeek = DayOfWeek.Sunday Then
                    x += 1
                End If
            End If
            x += 1
        Loop

Open in new window


Which does report correctly to an extent however it is also bringing in October dates where as I only want September.

The results I get from the above is

03 September 2012 - 07 September 2012
10 September 2012 - 14 September 2012
17 September 2012 - 21 September 2012
24 September 2012 - 28 September 2012
01 October 2012 - 05 October 2012
08 October 2012 - 12 October 2012
15 October 2012 - 19 October 2012
22 October 2012 - 26 October 2012
29 October 2012 - 31 October 2012


Any help in trying to solve this would be great please.

Thanks,
0
Comment
Question by:BoltonWanderer
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 420 total points
ID: 38823213
Try this out:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim dtestart As DateTime = DateTimePicker1.Value
        Dim weeks As List(Of String) = GetWeeks(dtestart)

        For Each week As String In weeks
            Debug.Print(week)
        Next
    End Sub

    Private Function GetWeeks(ByVal dt As DateTime) As List(Of String)
        Dim weeks As New List(Of String)

        Dim stopDT As DateTime
        Dim curDT As New DateTime(dt.Year, dt.Month, 1)
        While curDT.DayOfWeek = DayOfWeek.Saturday Or curDT.DayOfWeek = DayOfWeek.Sunday
            curDT = curDT.AddDays(1)
        End While

        While curDT.Year = dt.Year AndAlso curDT.Month = dt.Month
            stopDT = curDT
            While stopDT.DayOfWeek <> DayOfWeek.Friday
                stopDT = stopDT.AddDays(1)
            End While
            While stopDT.Month <> curDT.Month
                stopDT = stopDT.AddDays(-1)
            End While

            weeks.Add(curDT.ToString("d MMMM yyyy") & " - " & stopDT.ToString("d MMMM yyyy"))

            curDT = curDT.AddDays(1)
            While curDT.DayOfWeek <> DayOfWeek.Monday
                curDT = curDT.AddDays(1)
            End While
        End While

        Return weeks
    End Function

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38823234
Your problem is this line
dtestart = dtestart.AddDays(x + 3)
If that changes destart to the next month it will continue.

Probably the easiest way to fix it would just be to check if dtestart and dtestart.AddDays(x + 3) were the same month first and stop the loop in that case.
0
 

Author Closing Comment

by:BoltonWanderer
ID: 38823987
Worked perfectky. Thank you.
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

Suggested Solutions

Title # Comments Views Activity
Change data in datatable 8 30
DataGridView Events ? 3 37
How to use odbc in vb to connect to ms sql 14 37
Access to class from any project within a solution. 6 14
Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

23 Experts available now in Live!

Get 1:1 Help Now