拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 vb.net新年第四个星期二

vb.net新年第四个星期二

白鹭 - 2022-02-16 1948 0 0

我撰写了一个支票簿应用程序,该应用程序将本月的第 4 个星期二存盘在 SQLite 数据库中,当用户写支票并且今天的日期大于存盘的日期时,数据库中会更新新的第 4 个星期二的日期和一个函式存款 SS 付款已加载。
在测验时,我发现当年份更改时,我撰写的第一个函式会失败。
所以我写了第二个函式来处理 Years 的变化。
该代码似乎正在运行。我想将测验合并为一个函式,因为代码看起来不够优雅。我将在下面发布小的 TEST 应用程序代码。这两个功能在一个模块中。

Public Class frmStart
Dim varSearchDate As Date
Dim varFTue As Date

Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    tbBox1.Text = "2021-12-28" ' 2021-11-23 2021-12-28 TEST DATES
End Sub

Private Sub btnADD_Click(sender As Object, e As EventArgs) Handles btnADD.Click
    varSearchDate = CDate(tbBox1.Text)
    tbAns.Text = varSearchDate.ToString("M-d-yyyy")

    Dim dateToday = Date.Today
    Dim mo As String
    mo = varSearchDate.ToString("MM")

    If dateToday > varSearchDate And CInt(mo) <> 12 Then
        varFTue = CDate(FourthTueOfNextMonth(Date.Today).ToString("yyyy-M-d"))
        MsgBox("varFTue Next Mo " & varFTue)
        tbBox2.Text = varFTue.ToString("yyyy-M-d")
        'WriteNewFourthTue()
        'gvTxType = "SS Deposit"
    ElseIf dateToday > varSearchDate And CInt(mo) = 12 Then
        varFTue = CDate(FourthTueOfNewYear(Date.Today).ToString("yyyy-M-d"))
        MsgBox("varFTue New Yr " & varFTue)
        tbBox3.Text = varFTue.ToString("yyyy-M-d")
        'WriteNewFourthTue()
        'gvTxType = "SS Deposit"
    End If
End Sub

结束班

这两个函式和我的 TEST 代码使用 ONLY ONE FUNCTION 注释掉

Module FunctionModule

'Function FourthTueOfNextMonth(dt As Date) As Date
'    Dim currDate = New Date(dt.Year, dt.Month, 1)
'    Dim nTuesday As Integer
'    While nTuesday < 4
'        If currDate.DayOfWeek = DayOfWeek.Tuesday Then
'            nTuesday  = 1
'        End If
'        currDate = currDate.AddDays(1)
'    End While
'    If dt.Month <> 12 Then
'        Return New Date(dt.Year, dt.Month, currDate.Day - 1)
'    ElseIf dt.Month = 12 Then
'        Return New Date(dt.Year   1, dt.Month - 11, currDate.Day - 1)
'    End If
'End Function

Function FourthTueOfNextMonth(dt As Date) As Date
    Dim currDate = New Date(dt.Year, dt.Month, 1)
    Dim nTuesday As Integer

    While nTuesday < 4
        If currDate.DayOfWeek = DayOfWeek.Tuesday Then
            nTuesday  = 1
        End If
        currDate = currDate.AddDays(1)
    End While

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

End Function

Function FourthTueOfNewYear(dt As Date) As Date
    Dim currDate = New Date(dt.Year   1, dt.Month - 11, 1)
    Dim nTuesday As Integer

    While nTuesday < 4
        If currDate.DayOfWeek = DayOfWeek.Tuesday Then
            nTuesday  = 1
        End If
        currDate = currDate.AddDays(1)
    End While

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

End Function

终端模块

我的问题有没有更好的方法来撰写这段代码,所以我只有一个函式?

uj5u.com热心网友回复:

这是一个函式,它根据传入的日期查找下个月的第四个星期二:

Function FourthTueOfNextMonth(dt As Date) As Date
    ' Start with the First Day of the Month, from the date passed in.
    ' Add one Month to that to get the first Day of the NEXT month.
    Dim currDate As Date = (New Date(dt.Year, dt.Month, 1)).AddMonths(1)
    ' Find the First Tuesday of the Month
    While currDate.DayOfWeek <> DayOfWeek.Tuesday
        currDate = currDate.AddDays(1)
    End While
    ' Add three more Weeks to jump to Fourth Tuesday
    Return currDate.AddDays(21)
End Function

请注意,该函式始终回传下个月的第四个星期二,并且它不会检查传入的日期是否小于包含传入日期的同月的第四个星期二。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *