471,357 Members | 1,085 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

calculate Monday's date

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
Aug 3 '05 #1
2 1837
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.

"Joecx" <Jo***@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
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

Aug 3 '05 #2
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

"Joecx" <Jo***@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
| 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
Aug 3 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Martien van Wanrooij | last post: by
2 posts views Thread by Rustan | last post: by
2 posts views Thread by Joecx | last post: by
5 posts views Thread by Chris | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.