开发者

Fixing VB6 Date Bug?

开发者 https://www.devze.com 2023-01-06 04:56 出处:网络
It seems that VB6 can\'t correctly compare dates in some situations.Are there any solutions to this? Private Sub CheckDate()

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, #开发者_StackOverflow7/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)?


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


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

Debug.Print "Equal? " + CStr(Abs(date1 - date2) < 0.000000001)
0

精彩评论

暂无评论...
验证码 换一张
取 消