Date Question

Posted on 2005-03-09
Medium Priority
Last Modified: 2010-04-23
If I had a simple form with one textbox and a listbox and I wanted to type the year into the textbox (ex. 2004 or 2005 etc) and have the listbox populate with
all the dates "??/??/????" that start each week.  Like all the Sundays.  How would that be done?

Any Suggestions?

Question by:swilli6926
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
LVL 12

Expert Comment

ID: 13500221
Ok, this may need more testing, but this seems to work (areas that may need to be tested is when the 31/12/xxxx is a sunday)

Sub ListAllSundays(ByVal YearNum As Integer, Byval ListX as Listbox)
        Dim i As Integer
        Dim thisDate As Date
        Dim firstSunday As Integer
        For i = 1 To 7
            thisDate = New Date(YearNum , 1, i)
            If thisDate.DayOfWeek = DayOfWeek.Sunday Then
                Exit For
            End If

        Do While thisDate.Year = YearNum
            thisDate = thisDate.AddDays(7)
End Sub
LVL 86

Accepted Solution

Mike Tomlinson earned 200 total points
ID: 13500236
Something like...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim year As Integer = Integer.Parse(TextBox1.Text)
            DayOfWeekForEntireYear(year, vbSunday)
        Catch ex As Exception
            MessageBox.Show(TextBox1.Text, "Invalid Year", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub DayOfWeekForEntireYear(ByVal year As Integer, ByVal DayOfWeek As Integer)
        If DayOfWeek < 1 Or DayOfWeek > 7 Then
            MessageBox.Show("DayOfWeek must be between 1 (Sunday) and 7 (Saturday)", "Invalid DayOfWeek", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

            Dim dt As DateTime = New DateTime(year, 1, 1)
            While DatePart(DateInterval.Weekday, dt) <> DayOfWeek
                dt = dt.AddDays(1)
            End While

            While dt.Year = year
                dt = dt.AddDays(7)
            End While
        Catch ex As Exception
            MessageBox.Show(TextBox1.Text, "Invalid Year", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
LVL 12

Expert Comment

ID: 13500250
Ok... Idle's is a lot neater than mine (and takes into account different days :P)

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 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