I'm trying to get the date of the monday in the current week. Is this close to correct?
Dim MondayOfCurrentWeek As Date = Date.Today - Date.Today.DayOfWeek + 1
As far as I understand it the AyOfWeek indexes are:
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
0 = Sunday
Hence if for example Date Today is a thursday I would get:
Dim MondayOfCurrentWeek As Date = Date - 4 + 1
which would be equal to
Date - 3
and seems right to me.
Or am I totally off?
A very simple alternative approach, which while probably not as performant, but does avoid any arithmetic based errors:
Dim monday As Date = Date.Today
While (monday.DayOfWeek <> DayOfWeek.Monday)
monday = monday.AddDays(-1)
End While
This could easily be extended out to a function to handle any day of the week. Unless this is a really high volume piece of code, performance will be fine.