Find the 4th Tue of the Month when the Year changes
-
I have written a Check Book application and the code that calculates the 4th Tue of the Month is failing when we enter a new Year. I have added an additional Function that works but the code seems less tan elegant. I have tested a few changes with no results. Without posting a lot of unnecessary code here is the process and the relevant code that works now. I am storing he 4th Tue of the month in a SQLite DB and when the user writes a check I am testing if the date today is greater than the stored DB date. If this test is true than I update the stored date to the 4th Tue for next Month. I would like to know if there is a better way to design this process ? Perhaps only using one Function.
Public Sub GetDateInfo() Using conn As New SQLiteConnection($"Data Source = '{gv\_dbName}';Version=3;") conn.Open() Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT \* FROM DateInfoTable WHERE DID = '1'", conn) Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader While rdr.Read() varSearchDate = CDate(rdr("diTESTdate")) End While End Using End Using End Using End Sub
This is the code that executes when user writes a check
Public Sub testDates() GetDateInfo() 'Other Option 'ElseIf dateToday > varSearchDate And CInt(varSearchDate.ToString.Substring(0, 2)) = 12 Then '============================================================================================ Dim dateToday = Date.Today Dim mo As String mo = dateToday.ToString("MM") 'mo = varSearchDate.ToString("MM") 'line above for testing for NOV If dateToday > varSearchDate And CInt(mo) <> 12 Then varFTue = CDate(FourthTueOfNextMonth(Date.Today).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 " & varFTue) WriteNewFourthTue() gvTxType = "SS Deposit" End If End Sub
These are the two Functions that are in a Data Module
Function FourthTueOfNextMonth(dt As Date) As Date Dim currDate = New Date(dt.Year, dt.Month, 1) Dim nTuesday As Integer While nTuesday < 4
-
I have written a Check Book application and the code that calculates the 4th Tue of the Month is failing when we enter a new Year. I have added an additional Function that works but the code seems less tan elegant. I have tested a few changes with no results. Without posting a lot of unnecessary code here is the process and the relevant code that works now. I am storing he 4th Tue of the month in a SQLite DB and when the user writes a check I am testing if the date today is greater than the stored DB date. If this test is true than I update the stored date to the 4th Tue for next Month. I would like to know if there is a better way to design this process ? Perhaps only using one Function.
Public Sub GetDateInfo() Using conn As New SQLiteConnection($"Data Source = '{gv\_dbName}';Version=3;") conn.Open() Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT \* FROM DateInfoTable WHERE DID = '1'", conn) Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader While rdr.Read() varSearchDate = CDate(rdr("diTESTdate")) End While End Using End Using End Using End Sub
This is the code that executes when user writes a check
Public Sub testDates() GetDateInfo() 'Other Option 'ElseIf dateToday > varSearchDate And CInt(varSearchDate.ToString.Substring(0, 2)) = 12 Then '============================================================================================ Dim dateToday = Date.Today Dim mo As String mo = dateToday.ToString("MM") 'mo = varSearchDate.ToString("MM") 'line above for testing for NOV If dateToday > varSearchDate And CInt(mo) <> 12 Then varFTue = CDate(FourthTueOfNextMonth(Date.Today).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 " & varFTue) WriteNewFourthTue() gvTxType = "SS Deposit" End If End Sub
These are the two Functions that are in a Data Module
Function FourthTueOfNextMonth(dt As Date) As Date Dim currDate = New Date(dt.Year, dt.Month, 1) Dim nTuesday As Integer While nTuesday < 4
If the "day" of the "Tuesday date" is >= 22 and <= 28, it's the 4th Tuesday. [How to: Extract the Day of the Week from a Specific Date | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
If the "day" of the "Tuesday date" is >= 22 and <= 28, it's the 4th Tuesday. [How to: Extract the Day of the Week from a Specific Date | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
Gerry I like the idea. Guess I was being exact with the date for each month in case the user does not write a check between the date range. With the exact date from the database if the user writes a check after the 28 of last month the the event will trigger in the next month. I was trying to avoid having two Function and find a better way to write a test that would capture the 4th Tue when the YEAR changes. I appreciate the link to working with Date's Date's are like a moving target that always needs to go back to the starting point January
-
I have written a Check Book application and the code that calculates the 4th Tue of the Month is failing when we enter a new Year. I have added an additional Function that works but the code seems less tan elegant. I have tested a few changes with no results. Without posting a lot of unnecessary code here is the process and the relevant code that works now. I am storing he 4th Tue of the month in a SQLite DB and when the user writes a check I am testing if the date today is greater than the stored DB date. If this test is true than I update the stored date to the 4th Tue for next Month. I would like to know if there is a better way to design this process ? Perhaps only using one Function.
Public Sub GetDateInfo() Using conn As New SQLiteConnection($"Data Source = '{gv\_dbName}';Version=3;") conn.Open() Using cmd As SQLiteCommand = New SQLiteCommand($"SELECT \* FROM DateInfoTable WHERE DID = '1'", conn) Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader While rdr.Read() varSearchDate = CDate(rdr("diTESTdate")) End While End Using End Using End Using End Sub
This is the code that executes when user writes a check
Public Sub testDates() GetDateInfo() 'Other Option 'ElseIf dateToday > varSearchDate And CInt(varSearchDate.ToString.Substring(0, 2)) = 12 Then '============================================================================================ Dim dateToday = Date.Today Dim mo As String mo = dateToday.ToString("MM") 'mo = varSearchDate.ToString("MM") 'line above for testing for NOV If dateToday > varSearchDate And CInt(mo) <> 12 Then varFTue = CDate(FourthTueOfNextMonth(Date.Today).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 " & varFTue) WriteNewFourthTue() gvTxType = "SS Deposit" End If End Sub
These are the two Functions that are in a Data Module
Function FourthTueOfNextMonth(dt As Date) As Date Dim currDate = New Date(dt.Year, dt.Month, 1) Dim nTuesday As Integer While nTuesday < 4
Use date add with 3 weeks and add weekday difference with it you will get the fourth Tuesday
Every thing is possible
-
Use date add with 3 weeks and add weekday difference with it you will get the fourth Tuesday
Every thing is possible
After consideration I have rewritten the Functions with just this one. Still testing but I believe it will correct the issue when the year changes.
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) 'Dim currDate As Date = (New Date(2022, 1, 1)).AddMonths(1) 'LINE ABOVE for testing set it to todays DATE ' 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