calculate Monday's date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to give a date (not today's date) and have the program
calculate the previous monday's date from it? Any help would be appreciated.
Thanks!!!!

Joe
 
The DateTime object as a DayOfWeek property. Just keep subtracting 1 day
from the date and check that property. Once you hit Monday, you are done.
 
Joe,
As Marina I normally subtract the difference between your desired Date's day
of week & Monday's day of week.

Something like:

Public Function MondayBefore(ByVal day As DateTime) As DateTime
Dim days As Integer = day.DayOfWeek - DayOfWeek.Monday
If days < 0 Then
days += 7
End If
Return day.Date.Subtract(TimeSpan.FromDays(days))
End Function

Then you can call it passing in any date you want:

Debug.WriteLine(MondayBefore(DateTime.Now))
Debug.WriteLine(MondayBefore(#12/25/2005#))


Hope this helps
Jay

| Does anyone know how to give a date (not today's date) and have the
program
| calculate the previous monday's date from it? Any help would be
appreciated.
| Thanks!!!!
|
| Joe
 
Back
Top