简体   繁体   中英

vb.net find 4th Wednesday of month with visual basic

I am trying to find the fourth Wednesday of the current Month and Year when a form loads
I have converted numerous C# code to Visual Basic with no results
Would someone explain what is wrong with this code OR explain how to accomplish this in VB Code

    Private Sub SurroundingSub()
    Dim thisDate As Date = Today

    tbMonth.Text = thisDate.ToString("MMMM")
    tbYear.Text = thisDate.ToString("yyyy")
    Dim month As Integer
    month = tbMonth.Text
    Dim year As Integer
    year = tbYear.Text
    Dim fourthWed As Date = New Date(year, month, 4)
    tbFour.Text = fourthWed.ToLongDateString

    While fourthWed.DayOfWeek <> DayOfWeek.Wednesday
        fourthWed = fourthWed.AddDays(1)
        tbFour.Text = fourthWed.ToShortDateString
    End While

End Sub

This is a JavaFX statement that I am trying to implement in VB.Net

if(TorF.equals("F") && today.isAfter(fourthTUEofMONTH)) 

This sets the date

    public void setDATES() throws IOException, SQLException{
    today = LocalDate.now();
    fourthTUEofMONTH = LocalDate.now();
    fourthTUEofMONTH = fourthTUEofMONTH.with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.TUESDAY));
    endMONTH = LocalDate.now();
    endMONTH = endMONTH.with(TemporalAdjusters.lastDayOfMonth());
}

For a more general solution, with my function you can easily find the first, second, third, forth or fifth sunday, monday, tuesday, wednesday or whatever you want:

Public Function GetXDayOfWeek(Day As DateTime,
                              DayOfWeek As DayOfWeek, 
                              Optional Index As Integer = 1) As DateTime
    Dim First As New Date(Day.Year, Day.Month, 1)
    Dim Diff As Integer = (DayOfWeek - First.DayOfWeek + 7) Mod 7

    Return First.AddDays(Diff + (Index - 1) * 7)
End Function

So if you want to find the forth wednesday of the current month, use it like:

Dim DateToFind As DateTime = GetXDayOfWeek(Today, DayOfWeek.Wednesday, 4)

Your while statement will stop on the first Wednesday it finds, not the fourth. Keep track of the number of Wednesdays you encounter as you iterate and once you find the fourth then you can update tbFour.

Also as mentioned in the comments you'll want to start at the first day of the year.

Dim fourthWed As Date = New Date(year, month, 1)

Dim wednesdayCursor As Integer = 0

While wednesdayCursor < 4

    If fourthWed.DayOfWeek = DayOfWeek.Wednesday Then

        wednesdayCursor += 1
    
    End If

    fourthWed = fourthWed.AddDays(1)

End While

'Subtract one day because we added one on loop:
fbFour.Text = fourthWed.AddDays(-1).ToShortDateString()

You should make the function of getting the fourth Wednesday into a separate method, perhaps generalizing it for any day of the week, but just for the fourth Wednesday...

Module Module1

    Function FourthWedOfMonth(dt As DateTime) As Integer
        Dim currDate = New DateTime(dt.Year, dt.Month, 1)
        Dim nWednesdays = 0

        While nWednesdays < 4
            If currDate.DayOfWeek = DayOfWeek.Wednesday Then
                nWednesdays += 1
            End If
            currDate = currDate.AddDays(1)
        End While

        Return currDate.Day - 1

    End Function

    Sub Main()
        For mo = 1 To 12
            Console.Write(FourthWedOfMonth(New DateTime(2020, mo, 17)) & " ")
        Next

        Console.ReadLine()

    End Sub

End Module

Outputs the correct days for 2020:

22 26 25 22 27 24 22 26 23 28 25 23

If you wanted the DateTime of the fourth Wednesday, you could

Function FourthWedOfMonth(dt As DateTime) As DateTime
    Dim currDate = New DateTime(dt.Year, dt.Month, 1)
    Dim nWednesdays = 0

    While nWednesdays < 4
        If currDate.DayOfWeek = DayOfWeek.Wednesday Then
            nWednesdays += 1
        End If
        currDate = currDate.AddDays(1)
    End While

    Return New DateTime(dt.Year, dt.Month, currDate.Day - 1)

End Function

and then Console.WriteLine(FourthWedOfMonth(DateTime.Today).ToString("dd-MMM-yyyy")) would output "22-Jul-2020" (at the time of writing).

I'd find the fourth Wednesday of the current month this way:

Private Sub FourthWednesdayOfCurrentMonth()
    Dim firstOfMonth As DateTime = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1))
    Dim firstWednesday As DateTime = firstOfMonth.AddDays((7 + (DayOfWeek.Wednesday - firstOfMonth.DayOfWeek)) Mod 7)
    Dim fourthWednesday As DateTime = firstWednesday.AddDays(21)

    tbYear.Text = fourthWednesday.Year
    tbMonth.Text = fourthWednesday.Month
    tbFour.Text = fourthWednesday.Day
End Sub

Written generically for any day of the week, that would change to:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim fourthWednesday As DateTime = FourthWeekDayOfCurrentMonth(DayOfWeek.Wednesday)
    tbYear.Text = fourthWednesday.Year
    tbMonth.Text = fourthWednesday.Month
    tbFour.Text = fourthWednesday.Day
End Sub

Private Function FourthWeekDayOfCurrentMonth(ByVal WeekDay As DayOfWeek) As DateTime
    Dim firstOfMonth As DateTime = DateTime.Today.AddDays(-1 * (DateTime.Today.Day - 1))
    Dim firstWeekday As DateTime = firstOfMonth.AddDays((7 + (WeekDay - firstOfMonth.DayOfWeek)) Mod 7)
    Return firstWeekday.AddDays(21)
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM