Click here to Skip to main content
14,579,769 members
Rate this:
Please Sign up or sign in to vote.
I would like to create a visual basic form with a textbox in which a date would be entered. By pressing a button all the years would be pasted in a label where the entered date is on the same weekday.
For example date 4th of May in which of the next 100 years is on Monday?
I'm quite stuck with the loop.

What I have tried:

Dim birthdate As Date
Dim lastyear As Date = birthdate.AddYears(100)

Do While birthdate <= lastyear
birthdate = birthdate.AddDays(1)


Loop
Posted
Updated 21-May-20 15:43pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

1. Retrieve birthday from textbox.
2. Create a variable to store the DayOfWeek from the birthday.
3. Now go into your loop....
4. Check the DayOfWeek property and compare to what you stored in your variable.
5. Add a year
6. Close the loop


DayOfWeek reference:
DateTime.DayOfWeek Property (System) | Microsoft Docs[^]
   
Comments
Richard Deeming 22-May-20 6:56am
   
Remembering to take extra care if the date is 29th February, since AddYears(1) will return 28th February for the following year. :)
bobdetroit81 22-May-20 17:12pm
   
I've created the following code:
Dim birthdate As Date = TextBox2.Text
Dim a As Integer = birthdate.DayOfWeek

Dim lastyear As Date = birthdate.AddYears(100)
Dim count As Integer

Do While birthdate <= lastyear
birthdate = birthdate.AddYears(1)
If birthdate.DayOfWeek = a Then
count = count + 1
ListBox1.Items.Add(birthdate.Year)
End If
Loop


I've tested it, it works. Then I read your comment. I've tested the what happens if the birthday is on 2/29...Then I understood your comment, and I'm confused now... :(
MadMyche 22-May-20 17:44pm
   
Well... this is the time for an Executive Decision on what should be done for that special case, and then re-code accordingly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100