繁体   English   中英

vb.net 使用 Visual Basic 查找每月的第四个星期三

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

当表单加载时,我试图找到当前月份和年份的第四个星期三
我已将许多 C# 代码转换为 Visual Basic,但没有结果
有人能解释一下这段代码有什么问题吗,或者解释一下如何在 VB 代码中完成这个

    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

这是我试图在 VB.Net 中实现的 JavaFX 语句

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

这设置了日期

    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());
}

对于更通用的解决方案,使用我的 function 您可以轻松找到第一个、第二个、第三个、第四个或第五个星期日、星期一、星期二、星期三或任何您想要的:

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

因此,如果您想找到当月的第四个星期三,请像这样使用它:

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

您的 while 语句将在它找到的第一个星期三停止,而不是第四个。 跟踪迭代时遇到的星期三的数量,一旦找到第四个,就可以更新 tbFour。

正如评论中提到的,您希望从一年的第一天开始。

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()

您应该将获得第四个星期三的 function 变成一个单独的方法,也许将其推广到一周中的任何一天,但仅适用于第四个星期三......

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

输出 2020 年的正确日期:

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

如果你想要第四个星期三的 DateTime,你可以

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

然后Console.WriteLine(FourthWedOfMonth(DateTime.Today).ToString("dd-MMM-yyyy"))将 output "22-Jul-2020" (在撰写本文时)。

我会这样找到本月的第四个星期三:

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

一般写在一周中的任何一天,这将变为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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