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

GetLast

Gets the prior occurrence of the day of the week specified.

Source

Public Module DateTimeExtensions

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

End Module

Example

#11/5/2010#.GetLast(DayOfWeek.Tuesday) ' Returns #11/2/2010#

Author: M

Submitted on: 5 nov 2010

Language: VB

Type: System.DateTime

Views: 2340