locked
Finding the following Wednesday in ASP.NET RRS feed

  • Question

  • User-1881012951 posted

    Given a Date, how can I get the date of the following Wednesday (or any other weekday) in VB.NET.

    Thanks in advance

    Thursday, March 26, 2015 1:09 PM

Answers

  • User281315223 posted

    You could take a very basic approach and simply iterate through the days until you reach the next Wednesday (or any other specified day you are looking for) :

    Public Function GetNextDayOfType(ByVal currentDate As DateTime, ByRef dayOfWeek As DayOfWeek) As DateTime
    		' Store a reference for the current date '
    		Dim resultDate = currentDate
    		
    		' Begin iterating until you get to the appropriate day of the week '
    		While resultDate.DayOfWeek <> dayOfWeek
    			' Increment the day '
    			resultDate = resultDate.AddDays(1)
    		End While
    	
    		' We hit the day we are looking for, output it '
    		Return resultDate
    End Function

    You could use this as :

    ' Find the next Wednesday from Today '
    Dim nextWednesday = GetNextDayOfType(DateTime.Now, DayOfWeek.Wednesday)

    You can see a working example here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 26, 2015 1:25 PM

All replies

  • User281315223 posted

    You could take a very basic approach and simply iterate through the days until you reach the next Wednesday (or any other specified day you are looking for) :

    Public Function GetNextDayOfType(ByVal currentDate As DateTime, ByRef dayOfWeek As DayOfWeek) As DateTime
    		' Store a reference for the current date '
    		Dim resultDate = currentDate
    		
    		' Begin iterating until you get to the appropriate day of the week '
    		While resultDate.DayOfWeek <> dayOfWeek
    			' Increment the day '
    			resultDate = resultDate.AddDays(1)
    		End While
    	
    		' We hit the day we are looking for, output it '
    		Return resultDate
    End Function

    You could use this as :

    ' Find the next Wednesday from Today '
    Dim nextWednesday = GetNextDayOfType(DateTime.Now, DayOfWeek.Wednesday)

    You can see a working example here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 26, 2015 1:25 PM
  • User281315223 posted

    I was thinking about this a bit more and if you wanted a slightly cleaner implementation or a least one without a loop, you could use :

    Public Function GetNextDayOfType(ByVal currentDate As DateTime, ByRef dayOfWeek As DayOfWeek) As DateTime
    		' Store a reference for the current date '
    		Dim resultDate = currentDate
    		
    		' Get the difference in days of the week ' 
    		Dim difference = If(currentDate.DayOfWeek >= dayOfWeek, 7 - (currentDate.DayOfWeek - dayOfWeek), dayOfWeek - currentDate.DayOfWeek)
    	
    		' We hit the day we are looking for, output it '
    		Return resultDate.AddDays(difference)
    End Function

    You can see this example here as well.

    Thursday, March 26, 2015 2:42 PM