Solved

Comparing two dates dimmed as objects in VB.NET

Posted on 2011-03-05
14
638 Views
Last Modified: 2012-08-14
Hello Experts -

I am trying to comparing two dates in VB.NET, but I can't get the code to work. Here is the code snippet:

Dim prevdatetime As Object
Dim prevtime As Object
Dim testtime As Object

prevdatetime = #2/28/2011 8:45:00 PM#
prevtime = prevdatetime.ToLongTimeString      (Gives me 8:45:00 PM)
testtime = #12:00:00 PM#

If CDate(prevtime) < CDate(testtime) Then
             blah, blah, blah...                                  (The code doesn't get here.)
End If

Got any idea what I'm doing wrong?
0
Comment
Question by:gbmcneil
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 63 total points
Comment Utility
Not to state the obvious, but isn't 12:00:00PM (ie. noon) earlier than 8:4:00PM (at night) so the test is expected to fail since prevtime (at night) is > testtime (noon)?
0
 
LVL 10

Expert Comment

by:Jacco
Comment Utility
Tip: Never do unnessary conversions from text to date and reverse, only explicitly do this when you get the data from its source or write it to its destination. (So best not to use CDate(stringVar))

12:00PM is equal to the smallest time 0:00AM (you might use "11:59:999 PM" for upper bound, but this will essentially always render true)

Another approach in stead of working with DateTimes with "zero" dates is to use them with an equal date (or in case of 12:00PM one day ahead)

Regards, Jacco

0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
0
 
LVL 10

Expert Comment

by:Jacco
Comment Utility
my bad :-)
0
 
LVL 9

Expert Comment

by:Orcbighter
Comment Utility
Your problem is that TestTime is a Time and not a date.
Its the same structure but the year month and day are set to min values.
To make it work, and I assume that since you were comparing a time, you were assuming that the date was Today, You should set the testTime variable to Now, then modify the time to what you want, then do the comparison.
Or, if you really only want to compare times, zero-out the year, month and day values.
See an example in the code snippet below:

Dim prevdate As DateTime = #2/28/2011 8:45:00 PM#
        Dim testdate As DateTime = Now

        'if current time is 11.30 PM, and you want 12pm (midnight, ie 0 on 24-hour scale) then just modify the hours
        testdate.AddHours(1)
        testdate.AddMinutes(30)

        If DateTime.Compare(prevdate, testdate) > 0 Then
            Console.WriteLine("prevdate > testdate")
        ElseIf DateTime.Compare(prevdate, testdate) = 0 Then
            Console.WriteLine("prevdate == testdate")
        ElseIf DateTime.Compare(prevdate, testdate) < 0 Then
            Console.WriteLine("prevdate < testdate")
        End If

Open in new window

0
 

Author Comment

by:gbmcneil
Comment Utility
My eight lines of code certainly took a bashing. Does this approach represent an improvement to you guys?

Dim prevdatetime As DateTime
Dim prevtime As DateTime
Dim testtime As DateTime

prevdatetime = #2/28/2011 8:45:00 PM#
prevtime = CDate(prevdatetime.ToLongTimeString)      '(Gives me 8:45:00 PM)
testtime = #11:59:59 PM#

If prevtime <= testtime Then
                'blah, blah, blah...                                            '(Stop triggered)
                Stop
End If

By the way, it never occurred to me that 12:00 midnight was 12:00 AM. It sounds illogical. Also, the test may sound nonsensical. That is because it is taken out of context and is considerably more complex in real life. I'm just trying to simplify things for discussion.
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
Looks a little odd to me.  How does testtime string get converted to a DateTime?  Is there an implied conversion?  Orcbighter, what do you say?

Question is, does it work?

The logic I use to explain why noon should be 12PM is that 12:00:01 after noon is clearly PM, and truncating the seconds or minutes from the display should not change it from PM to AM.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 62 total points
Comment Utility
Just to show a different method, I would do:
Dim prevdatetime As DateTime = #2/28/2011 8:45:00 PM#

        If prevdatetime.TimeOfDay <= New TimeSpan(23, 59, 59) Then ' #11:59:59 PM#
            'blah, blah, blah...                                            '(Stop triggered)
            MessageBox.Show("Stop: Less Than or Equal to 11:59:49 PM")
        End If

Open in new window


*But* all TIMES are less than 11:59:59 PM by definition.  So the check is pointless...
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
Comment Utility
Hi!

First thing you need to do is to use CORRECT DATA TYPE.

like, change this:
        Dim prevdatetime As Object
        Dim prevtime As Object
        Dim testtime As Object

to
        Dim prevdatetime As DateTime
        Dim prevtime As String
        Dim testtime As DateTime

then final code should look like this:
     
   Dim prevdatetime As DateTime
        Dim prevtime As String
        Dim testtime As DateTime

        prevdatetime = #2/28/2011 8:45:00 PM#
        prevtime = prevdatetime.ToLongTimeString      '(Gives me 8:45:00 PM)
        testtime = #12:00:00 AM#

        If CDate(prevtime) < testtime Then
            ' blah, blah, blah...                                  (The code doesn't get here.)
        End If

Open in new window

0
 

Author Comment

by:gbmcneil
Comment Utility
Gee, guys you're making me feel hurt. Like I should have gone into the landscaping business or something.

You're saying that a DateTime variable has got to have both a date and a time. A variable that has only one of these elements doesn't qualify. So, you can't compare dates to dates and times to times unless you've got the whole enchilada.

So, you have to use attributes like TimeOfDay to make comparisons. Okay. I can appreciate the 12:00 AM midnight argument too.

By the way, the code does work - but, it may be sheer luck.

0
 

Author Comment

by:gbmcneil
Comment Utility
Okay. If variable "testtime" can be type DateTime, then why can't variable "prevtime" be type DateTime too?  (NOTE: And, iif testtime only indicates a time, then someone other than me says it can be dimmed as DateTime.)

In one of my examples I suggested:

Dim prevtime As DateTime
prevtime = CDate(prevdatetime.ToLongTimeString)

...and then continued to make the comparison:

If prevtime <= testtime Then
                'blah, blah, blah...
End If

That is the only difference between my program snippet and Shahan Developer's.  

But, the consensus was that this was wrong. I think I'm confused. Hold up on that rake!


0
 
LVL 19

Expert Comment

by:Shahan Ayyub
Comment Utility
Hi! @gbmcneil

Both are the same things, but the difference is the line, where operations performed. I didn't see the post that you have almost similar of my post because I didn't refreshed this page and posted you the code.

>>>If variable "testtime" can be type DateTime, then why can't variable "prevtime" be type DateTime too?

In your original question, "prevdatetime.ToLongTimeString" returns "string" so i suggested it to declare as a type "string". Everyone who is providing you a code can be an alternative of your requirement, it doesn't mean to hurt you. You can ask for clarification from that expert that why they suggested it.

My apologies if you are confused from my comment.
0
 

Author Comment

by:gbmcneil
Comment Utility
My comments addressed the revised code I presented 8 panels down from the top of this question.

What you seem to be suggesting is that "there is more than one way to slice a tomato". And, there very well may be.

But, I think that Idle Mind opines that there is no need to create unnecessary variables when you can just use the "attributes" of the first variable presented that was given the type DateTime. In my example, this was prevdatetime. Idle say if you want the date alone, say prevdatetime.Date. Or if you want the time, it can be had by using prevdatetime.TimeOfDay.

Other than the 12:00 AM midnight brew-hah-hah, I think that this is what this question boils down to.
0
 

Author Closing Comment

by:gbmcneil
Comment Utility
A great discussion. I learned a lot. Too bad I don't have more points to hand out. All the responses were great and I appreciate them immensely.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now