Home > Software design >  VB.Net find last day of month with date format
VB.Net find last day of month with date format

Time:10-02

I am trying to find the last day of the month and compare it to today's date
I do NOT want the integer number I would like the result in this format "MM-dd-yyyy"
Date Picker will not work for this project
Here is the code I using but the process seems overly complicated concocting strings
Side note when today is after the 4th Tue I write True and the Last Day of the month to a DB
when today is after the last day of the month and the bool is now True I write the new last day of the new month and false to the DB

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

    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
   Private Sub btnFindDate_Click(sender As Object, e As EventArgs) Handles btnFindDate.Click

    Dim tORf As Boolean = False
    Dim dateToday = Date.Today
    Dim dateFourthTue = (FourthTueOfMonth(Date.Today))

    tbFourthTue.Text = dateFourthTue.ToString("MMM-dd-yyyy")
    tbThree.Text = dateFourthTue.ToString("yyyy-MM-dd")

    tbEndOFMonth.Text = Date.DaysInMonth(Date.Now.Year, Date.Now.AddMonths(0).Month).ToString

    Dim dToday As Date
    dToday = Date.Parse("10-01-2021")
    Dim dtY = dateToday.ToString("yyyy")
    Dim dtM = dateToday.ToString("MM")
    Dim eom As String = Date.DaysInMonth(Date.Now.Year, Date.Now.AddMonths(0).Month).ToString
    Dim dtALL As String
    dtALL = dtM & "-" & eom & "-"   dtY
    Dim testD As Date
    testD = Date.Parse(dtALL)

    If tORf = False And dToday > dateFourthTue Then
        MessageBox.Show("Today > Fourth Tue")
        'tORf = True'Write True
        'tbMessage.Text = tORf.ToString
    End If

    If tORf = True And dToday > testD Then
        MessageBox.Show("Today > End Of Last Month")
        'tORf = False write False
        'tbMessage.Text = tORf.ToString
    End If
    End Sub

CodePudding user response:

I hate to admit I might have gave up the search too quick
found the answer here
Answer Here

Here is the code

    Dim dateToday = Date.Now.AddMonths(0)
    Dim dateEndOfMonth = New Date(dateToday.Year, dateToday.Month, DateTime.DaysInMonth(dateToday.Year, dateToday.Month))
    tbMsg.Text = dateEndOfMonth.ToString("MM-dd-yyyy")

Code seems to be working ?
I have seen suggestions to use this format for comparing dates
TEST DATES use this format yyyyMMdd Please comment if you can add to the answer

CodePudding user response:

A few things:

You want to use today - not "now()" as that includes a time portion. While a date type only has date, you should consider if you have datetime, and either way, no need to introduce and use a value that includes both date and time such as now does.

I reocmmend this code:

    Dim dtToday As Date = Date.Today

    Dim dtEndOfMonth As Date = DateSerial(dtToday.Year, dtToday.Month   1, 0)

    Debug.Print(dtToday)
    Debug.Print(dtEndOfMonth)

Output: Today happens to be the 1st, but any date would work. This includes end of year, and even leap years.

2021-10-01
2021-10-31

So, this is a long time old trick - goes back to old VB6, and even old VBA code from 20 years ago.

So, we use date serial to produce a date, but if you use 0 for the day, then you get the previous day, and thus gets you the last day of the current month.

So we toss in year, month 1, and 0 for the date - that results in the last day of the current month.

  • Related