Link to home
Start Free TrialLog in
Avatar of gbmcneil
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.
Avatar of gamarrojgq
gamarrojgq

Hi,

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


Avatar of gbmcneil

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.ToString     (Gives me "Monday")
            prevdate = nowdatetime      ?                               (Looking for "2/28/2011")
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.ToString     (Gives me "Monday")
prevdate = nowdatetime.ToString("MM/dd/yyyy")                               (Looking for "2/28/2011
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.ToString     (Gives me "Monday")
prevdate = prevdatetime.ToString("MM/dd/yyyy")                               (Looking for "2/28/2011

What am I doing wrong here?
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.ToString         (Gives me "Monday")
prevdate = prevdatetime.ToShortDateString             (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.


Avatar of Mike Tomlinson
Declare your variables with the correct data types:
Dim prevdatetime As DateTime
        Dim prevday As String
        Dim prevdate As String

Open in new window

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.DayOfWeek.ToString)         (Gives me "Monday")
prevdate = CDate(prevdatetime.ToShortDateString)             (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?  

This line:

    prevday = CDate(prevdatetime.DayOfWeek.ToString)

Will cause an exception.  How do you convert "Monday" to a Date?



This line:

    prevdate = CDate(prevdatetime.ToShortDateString)

Could be replace with:

    prevdate = prevdatetime.Date

Why convert to a String just to convert back to a date?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You're saying don't make them separate variables. Just deal with them in their "attributed" form.

I think I got it now.
And, can you compare ThisDateTime.TimeOfDay with ThatDateTime.TimeOfDay?

That is:

If ThisDateTime.TimeOfDay > ThatDateTime.TimeOfDay Then
     'blah, blah, blah...
End If


 
You are a big help as usual, Idle.