GetNext

Public Module DateTimeExtensions

	''' <summary>
	''' Returns the next occurrence of the day of the week specified.
	''' </summary>
	<Extension()> _
	Public Function GetNext(ByVal dt As DateTime, ByVal dayOfWeek As DayOfWeek) As DateTime
		Dim daysToAdd As Integer = 0
		If dt.DayOfWeek < dayOfWeek Then
			daysToAdd = dayOfWeek - dt.DayOfWeek
		Else
			daysToAdd = (7 - dt.DayOfWeek) + dayOfWeek
		End If
		Return dt.AddDays(daysToAdd)
	End Function

End Module
Example:
#1/1/2010#.GetNext(DayOfWeek.Friday) ' Returns #1/5/2010#

Description

Gets the next occurrence day of the week specified.

Details

Double click on the code to select all.

 

;