Muhammad Ali
Muhammad Ali

Reputation: 873

Get Sunday Of Given Start Date And Saturday Of End Date

I want to change my starting date to Sunday of the StartDate and EndDate to Saturday of EndDate.Suppose my StartDate is 23-04-2014,it will become 20-04-2013 and my EndDate is 13-05-2014,it will become 17-05-2014.How can i achieve this is vb.net.In short what I want is to convert my StartDate to Sunday of given date week and EndDate to Saturday of given date week.

Upvotes: 0

Views: 2228

Answers (1)

rory.ap
rory.ap

Reputation: 35338

Will these work for your purposes?

Function GetPreviousSunday(fromDate As Date) As Date
    Return fromDate.AddDays(0 - fromDate.DayOfWeek)
End Function

Function GetNextSaturday(fromDate As Date) As Date
    Return fromDate.AddDays(6 - fromDate.DayOfWeek)
End Function

You could even turn it into one function:

Function GetRelativeWeekdayDate(fromDate As Date, relativeToWeekdayNum As Integer) As Date
    Return fromDate.AddDays(relativeToWeekdayNum - fromDate.DayOfWeek)
End Function

Which you would call like this for Sunday: GetRelativeWeekdayDate(StartDate, 0) and this for Saturday: GetRelativeWeekdayDate(EndDate, 6).

Upvotes: 2

Related Questions