Hey,
How would I calculate the Last Week of today's date?
I don't need the last 7 days, but the last week.
Thanks,
Printable View
Hey,
How would I calculate the Last Week of today's date?
I don't need the last 7 days, but the last week.
Thanks,
As in.. just subtract 7 days??? If so, then use the .Subtract method of the Date class...
Code:'todays date
Dim MyDate As Date = Date.Now
'below, subtracts 7 days from MyDate
Dim OldDate As Date = MyDate.Subtract(New TimeSpan(7, 0, 0, 0))
'Displays date from a week ago...
MessageBox.Show(OldDate.ToShortDateString)
no, I know how to subtract 7 days.
I want the dates of last week. (Monday to Sunday of last week)
Then just loop :)
Or do you want the first full monday to sunday...? And only monday to sunday?Code:Dim MyDate As Date = Date.Now
For I As Integer = 1 To 7 Step 1
Dim OldDate As Date = MyDate.Subtract(New TimeSpan(I, 0, 0, 0))
MessageBox.Show(OldDate.ToShortDateString)
Next
**EDIT - had -1 instead of 1...
Here's how to get the first full Moday to Sunday of the previous week...
Code:Dim MyDate As Date = Date.Now
For J As Integer = 7 To 1 Step -1
Dim OldDate As Date = MyDate.Subtract(New TimeSpan(J + MyDate.DayOfWeek - 1, 0, 0, 0))
MessageBox.Show(OldDate.ToShortDateString)
Next
What I need is to know the dates of last week.
If today is 4th of February, the dates I want are January 23 - 29.
That would be last week.
Then my last post above should do it :)
hehe, we posted at teh same time. the second one works fine. thanks a lot.
On a side note, to get the actual enumeration value that states "Monday", etc instead of the numbers, you can try something like this in the messagebox code above...
Code:'displays the value as "Monday, 01/23/2006"
MessageBox.Show(DayOfWeek.GetName(GetType(DayOfWeek), OldDate.DayOfWeek) & ", " & OldDate.ToShortDateString)