ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

GetNext

Gets the next occurrence day of the week specified.

Source

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#

Author: MattMc3

Submitted on: 5 nov 2010

Language: VB

Type: System.DateTime

Views: 2591