GetLast

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#

Description

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

Details

  • Author: M
  • Submitted on: 11/5/2010 8:25:08 PM
  • Language: VB
  • Type: System.DateTime
  • Views: 1376

Double click on the code to select all.

 

;