PRB: Date Comparison Can fail Using Arithmetic Operators (194894)
The information in this article applies to:
- Microsoft Visual Basic Learning Edition for Windows 5.0
- Microsoft Visual Basic Learning Edition for Windows 6.0
- Microsoft Visual Basic Professional Edition for Windows 5.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 5.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual Basic for Applications 5.0
- Microsoft Visual Basic Standard Edition for Windows 4.0
- Microsoft Visual Basic Professional Edition for Windows 4.0
- Microsoft Visual Basic Enterprise Edition for Windows 4.0
This article was previously published under Q194894 SYMPTOMS
Comparing two apparently identical dates using the standard comparison
operators produces anomalous results.
CAUSE
The Visual Basic Date data type is stored internally as a Double (64 bit
floating point). Performing operations on Date variables is subject to the
same rounding problems as any other floating point value. This is true even
if most of the intrinsic functions, such as DateAdd, are used.
RESOLUTION- Use the DateDiff intrinsic function to compare date and time values.
DateDiff rounds the input values to the specified tolerance, eliminating
floating point errors.
- Take the difference between two variables of type Date and compare the
absolute value of the result to a tolerance value such as 10E-11. If the
difference is less than the tolerance, the values may be considered
identical.
STATUS
This behavior is by design.
REFERENCES
For additional information, please see the following articles in the
Microsoft Knowledge Base:
130514
: ACC: Storing, Calculating, and Comparing Date/Time Data
42980
: (Complete) Tutorial to Understand IEEE Floating-Point Errors
Modification Type: | Major | Last Reviewed: | 6/29/2004 |
---|
Keywords: | kbcode kbDateTime kbprb KB194894 |
---|
|