我撰寫了一個支票簿應用程式,該應用程式將每月的第 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
請注意,該函式始終回傳下個月的第四個星期二,并且它不會檢查傳入的日期是否小于包含傳入日期的同月的第四個星期二。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399778.html
