|
-
Feb 4th, 2006, 12:09 AM
#1
Thread Starter
Frenzied Member
Dates
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,
Don't anthropomorphize computers -- they hate it
-
Feb 4th, 2006, 12:12 AM
#2
Re: Dates
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)
-
Feb 4th, 2006, 12:13 AM
#3
Thread Starter
Frenzied Member
Re: Dates
no, I know how to subtract 7 days.
I want the dates of last week. (Monday to Sunday of last week)
Don't anthropomorphize computers -- they hate it
-
Feb 4th, 2006, 12:15 AM
#4
Re: Dates
Then just loop 
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
Or do you want the first full monday to sunday...? And only monday to sunday?
**EDIT - had -1 instead of 1...
Last edited by gigemboy; Feb 4th, 2006 at 12:22 AM.
-
Feb 4th, 2006, 12:27 AM
#5
Re: Dates
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
-
Feb 4th, 2006, 12:28 AM
#6
Thread Starter
Frenzied Member
Re: Dates
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.
Don't anthropomorphize computers -- they hate it
-
Feb 4th, 2006, 12:29 AM
#7
Re: Dates
Then my last post above should do it
-
Feb 4th, 2006, 12:31 AM
#8
Thread Starter
Frenzied Member
Re: Dates
hehe, we posted at teh same time. the second one works fine. thanks a lot.
Don't anthropomorphize computers -- they hate it
-
Feb 4th, 2006, 12:36 AM
#9
Re: Dates
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|