简体   繁体   English

vb.net 新年第4个星期二

[英]vb.net 4th Tuesday when new year

I have written a Check Book application that stores the 4th Tuesday of the month in a SQLite Database and when the user writes a check and the Date Today is greater than the stored date a NEW 4th Tuesday date is updated in the DB and a function to deposit the SS Payment is loaded.我编写了一个支票簿应用程序,它将每月的第 4 个星期二存储在 SQLite 数据库中,当用户写支票并且今天的日期大于存储的日期时,数据库中更新了新的第 4 个星期二日期和 function 到存款 SS 付款已加载。
While testing I discovered that when the Year changes the first Function I wrote would fail.在测试时,我发现当年份更改时,我写的第一个 Function 会失败。
So I have written a second Function to deal with the the change in Years.所以我写了第二个 Function 来处理年的变化。
The code seems to be working.该代码似乎正在工作。 I would like to combine the test into one Function as the code seems less than elegant.我想将测试合并到一个 Function 中,因为代码看起来不够优雅。 I will post the small TEST application code below.我将在下面发布小型 TEST 应用程序代码。 The Two Functions are in a Module.这两个功能在一个模块中。

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

End Class结束 Class

The two function and my TEST code to use ONLY ONE FUNCTION commented out两个 function 和我的 TEST 代码仅使用一个 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

End Module端模块

My Question is there a better way to write this code so I only have one Function?我的问题有没有更好的方法来编写这段代码,所以我只有一个 Function?

Here's a function that finds the Fourth Tuesday of the NEXT Month, based on the Date passed in:这是一个 function,它根据传入的日期查找下个月的第四个星期二:

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

Note that the function always returns the fourth Tuesday of the NEXT month, and that it doesn't check to see if the date passed in is less than the Fourth Tuesday of the same month containing the date passed in.请注意,function 始终返回下个月的第四个星期二,并且它不会检查传入的日期是否小于包含传入日期的同月第四个星期二。

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

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