gbmcneil
asked on
The date of a previous date in VB.NET
I know how to get only the date of the System date, but not the date of a previous date.
Sounds confusing, but that's my problem.
Sounds confusing, but that's my problem.
ASKER
Thanks for responding gamarrojqq -
No. I want to extract the date of a previous known date/time.
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = nowdatetime.DayOfWeek.ToSt ring (Gives me "Monday")
prevdate = nowdatetime ? (Looking for "2/28/2011")
No. I want to extract the date of a previous known date/time.
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = nowdatetime.DayOfWeek.ToSt
prevdate = nowdatetime ? (Looking for "2/28/2011")
ASKER
I should mention that the date variables are previously dimmed as Objects.
Ok, using your example
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = nowdatetime.DayOfWeek.ToSt ring (Gives me "Monday")
prevdate = nowdatetime.ToString("MM/d d/yyyy") (Looking for "2/28/2011
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = nowdatetime.DayOfWeek.ToSt
prevdate = nowdatetime.ToString("MM/d
ASKER
A slight error in my example: nowdatetime should have been prevdatetime.
Nonetheless, when I run this I get "Conversion from string "MMddyyyy" to type 'Integer' is not valid."
Dim prevdatetime As Object
Dim prevday As Object
Dim prevdate As Object
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = prevdatetime.DayOfWeek.ToS tring (Gives me "Monday")
prevdate = prevdatetime.ToString("MM/ dd/yyyy") (Looking for "2/28/2011
What am I doing wrong here?
Nonetheless, when I run this I get "Conversion from string "MMddyyyy" to type 'Integer' is not valid."
Dim prevdatetime As Object
Dim prevday As Object
Dim prevdate As Object
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = prevdatetime.DayOfWeek.ToS
prevdate = prevdatetime.ToString("MM/
What am I doing wrong here?
ASKER
I got the following code to work -
Dim prevdatetime As Object
Dim prevday As Object
Dim prevdate As Object
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = prevdatetime.DayOfWeek.ToS tring (Gives me "Monday")
prevdate = prevdatetime.ToShortDateSt ring (Gives me "2/28/2011)
But, I will credit you with the answer if you can get your approach to work. We are all trying to learn here.
Dim prevdatetime As Object
Dim prevday As Object
Dim prevdate As Object
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = prevdatetime.DayOfWeek.ToS
prevdate = prevdatetime.ToShortDateSt
But, I will credit you with the answer if you can get your approach to work. We are all trying to learn here.
Declare your variables with the correct data types:
Dim prevdatetime As DateTime
Dim prevday As String
Dim prevdate As String
ASKER
Hi Idle -
Embelishing on your suggestion, why wouldn't I use this code instead?
Dim prevdatetime As DateTime
Dim prevday As DateTime
Dim prevdate As DateTime
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = CDate(prevdatetime.DayOfWe ek.ToStrin g) (Gives me "Monday")
prevdate = CDate(prevdatetime.ToShort DateString ) (Gives me "2/28/2011)
Wouldn't that put me in a position to compare these variables with other like variables if I wanted to? In otherwords, as a general rule in VB.NET dim all date and time variables as DateTime. Would I not dim them all as Variants in VB 6.0?
Embelishing on your suggestion, why wouldn't I use this code instead?
Dim prevdatetime As DateTime
Dim prevday As DateTime
Dim prevdate As DateTime
prevdatetime = #2/28/2011 8:45:00 PM#
prevday = CDate(prevdatetime.DayOfWe
prevdate = CDate(prevdatetime.ToShort
Wouldn't that put me in a position to compare these variables with other like variables if I wanted to? In otherwords, as a general rule in VB.NET dim all date and time variables as DateTime. Would I not dim them all as Variants in VB 6.0?
This line:
prevday = CDate(prevdatetime.DayOfWe ek.ToStrin g)
Will cause an exception. How do you convert "Monday" to a Date?
This line:
prevdate = CDate(prevdatetime.ToShort DateString )
Could be replace with:
prevdate = prevdatetime.Date
Why convert to a String just to convert back to a date?
prevday = CDate(prevdatetime.DayOfWe
Will cause an exception. How do you convert "Monday" to a Date?
This line:
prevdate = CDate(prevdatetime.ToShort
Could be replace with:
prevdate = prevdatetime.Date
Why convert to a String just to convert back to a date?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You're saying don't make them separate variables. Just deal with them in their "attributed" form.
I think I got it now.
I think I got it now.
ASKER
And, can you compare ThisDateTime.TimeOfDay with ThatDateTime.TimeOfDay?
That is:
If ThisDateTime.TimeOfDay > ThatDateTime.TimeOfDay Then
'blah, blah, blah...
End If
That is:
If ThisDateTime.TimeOfDay > ThatDateTime.TimeOfDay Then
'blah, blah, blah...
End If
ASKER
You are a big help as usual, Idle.
what you mean by "date of a previous date"?
If this means that you want to get the date of yesterday will be like this
Datetime.Today.AddDays(-1)
This will returns the "Date" of yesterday