PRB: Year Returned by DateSerial may be Unanticipated (197431)
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
This article was previously published under Q197431 SYMPTOMS
The DateSerial function can seem to return unanticipated results using
years represented by numeric expressions. For example, the following
evaluates to 1/1/100 and not 1/1/00 or 1/1/2000 because 99 + 1 or 100 is a
valid full size date:
DateSerial(99 + 1, 1, 1)
CAUSE
Visual Basic documentation gives the following rule concerning year
arguments used by the DateSerial function:
For the year argument, values between 0 and 29, inclusive, are
interpreted as the years 2000-2029. Values between 30 and 99, inclusive,
are interpreted as the years 1930-1999.
This rule only applies to year arguments represented with 1 or 2 digits.
This can sometimes produce results that may not be anticipated. For
example, in the following line of code the year is actually 100, which is a
valid 3 digit value in the range of 100 to 9999:
Therefore, DateSerial correctly returns the date 1/1/100. In this case
DateSerial will not return 1/1/00 or 1/1/2000. This type of behavior can
occur whenever the numeric expression in the year argument evaluates to a
year having more than 2 digits.
RESOLUTION
There are several ways to get the desired result (1/1/2000) using years
passed as numeric expressions. One way would be to use DateSerial with the
full 4 digit year. This example returns 1/1/00:
DateSerial(1999 + 1, 1, 1)
DateSerial can also be used in conjunction with the year function. This
example also returns 1/1/00:
DateSerial(Year(#1/1/99#)+1, Month(#1/1/99#), Day(#1/1/99#))
In addition to the DateSerial function, the DateAdd or DateDiff functions
can also be used. The following example also returns 1/1/00:
DateAdd("yyyy", 1, "1/1/99")
Modification Type: | Major | Last Reviewed: | 5/13/2003 |
---|
Keywords: | kbcode kbprb KB197431 |
---|
|