views:

48

answers:

2

It seems that VB6 can't correctly compare dates in some situations. Are there any solutions to this?

Private Sub CheckDate()

    date1 = #7/6/2010 2:00:00 PM#
    Debug.Print "Date 1: " + CStr(date1)

    date2 = DateAdd("h", -8, #7/6/2010 10:00:00 PM#)
    Debug.Print "Date 2: " + CStr(date2)

    Debug.Print "Equal? " + CStr(date1 = date2)

End Sub

The correct output should be:

Date 1: 7/6/2010 2:00:00 PM
Date 2: 7/6/2010 2:00:00 PM
Equal? True

but the real output is:

Date 1: 7/6/2010 2:00:00 PM
Date 2: 7/6/2010 2:00:00 PM
Equal? False

Is there any way around this, or is there any way to avoid this situation (whatever it is)?

+5  A: 

You should use the DateDiff function. It can be adapted to whatever level of precision you need.

http://www.vb6.us/tutorials/learn-howto-use-visual-basic-datediff-function

Andrew Lewis
Wow, that's a simple fix. Wish you didn't have to use it, but I'm glad it's there. Thanks!
derekerdmann
A: 

"Treat your dates as doubles" which they are behind the scene

Debug.Print "Equal? " + CStr(Abs(date1 - date2) < 0.000000001)
wqw
Yeah, that would work, but I like the solution that doesn't need casting.
derekerdmann
Ok, then you have datediff. Substraction of dates `Date - Date` is implicitly casted to `Double`, try `? typename(date1-date2)` in immediate window. This is like thinking that `Date = Date` is implicitly casted to `Boolean`! :-))
wqw