Incorrect in getting the date of every Thursday of current month in vb.net?

58 Views Asked by At

I tried to find out the Thursday of every week. It giving the right output if the current day is less than Thursday. And it gives wrong output if we are on Friday & Saturday.

 Private Function GetNextDate(day As DayOfWeek) As DateTime

        Dim now As DateTime = DateTime.Today
        Dim today As Integer = CInt(now.DayOfWeek)
        Dim find As Integer = CInt(day)

        Dim delta As Integer = find - today

        If delta > 0 Then
            Return now.AddDays(delta)

        Else
            Return now.AddDays(7 - delta)
        End If
    End Function

' For output
 Dim comingThursday = GetNextDate(DayOfWeek.Thursday)
        MsgBox(comingThursday.ToString)

Please help me out to find out the coming Thursday date properly. Or suggest me any other better way to find out.

3

There are 3 best solutions below

4
Étienne Laneville On

Your delta variable will be negative when the day you request (Thursday) is before the current day (Friday or Saturday) of the week. You therefore have to add it to 7, not subtract it:

now.AddDays(7 + delta)
3
djv On

Without worrying about the math, you can use LINQ to query the days in the current month, and filter on the day of the week you want

Private Function getDaysOfWeekInCurrentMonth(dayOfWeek As DayOfWeek) As IEnumerable(Of DateTime)
    Return Enumerable.
        Range(1, DateTime.DaysInMonth(Now.Year, Now.Month)).
        Select(Function(i) New DateTime(Now.Year, Now.Month, i)).
        Where(Function(d) d.DayOfWeek = dayOfWeek)
End Function
Dim thursdays = getDaysOfWeekInCurrentMonth(DayOfWeek.Thursday)
For Each thursday In thursdays
    Debug.WriteLine(thursday)
Next

7/6/2023 12:00:00 AM
7/13/2023 12:00:00 AM
7/20/2023 12:00:00 AM
7/27/2023 12:00:00 AM

0
dbasnett On

Some code that might help

''' <summary>
''' returns the date of the next DayOfWeek
''' </summary>
''' <param name="whDay">which DayOfWeek</param>
''' <param name="BasedOn">based on this date the next DayOfWeek</param>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetNextDOW(whDay As DayOfWeek,
                                BasedOn As Date) As Date
    Dim delta As Integer = whDay - BasedOn.DayOfWeek
    'depending on definition the comparison might just be .GT.
    ' as written if it is Wed and we are looking for the next Wed
    '  then skip ahead one week
    If BasedOn.DayOfWeek >= whDay Then
        delta += 7
    End If
    Dim rv As Date = BasedOn.AddDays(delta)
    ' Debug.WriteLine("The next {0} is {1} based on {2}", whDay, rv.ToShortDateString, BasedOn.ToShortDateString)
    Return rv
End Function

To test:

    Debug.WriteLine("")
    Dim dt As Date = #7/1/2023#
    Dim d As Date
    Do
        'all Monday's in July
        d = GetNextDOW(DayOfWeek.Monday, dt)
        If d.Month = dt.Month Then
            Debug.WriteLine("The next {0} is {1} based on {2}",
                                DayOfWeek.Monday, d.ToShortDateString, dt.ToShortDateString)
        End If
        dt = d
    Loop While dt.Month = 7

    Debug.WriteLine("")
    'first Thursday
    dt = #7/1/2023#
    If dt.DayOfWeek <> DayOfWeek.Thursday Then 'see comment in GetNextDOW
        d = GetNextDOW(DayOfWeek.Thursday, dt)
    End If

    'last Thursday
    dt = New Date(2023, 7, 31)
    If dt.DayOfWeek <> DayOfWeek.Thursday Then 'see comment in GetNextDOW
        dt = dt.AddDays(-7)
        d = GetNextDOW(DayOfWeek.Thursday, dt)
    End If