How To Prepare Visual Basic Applications for the Year 2000

This article was previously published under Q162718
This article has been archived. It is offered "as is" and will no longer be updated.
As a developer, you may be concerned about how your applications willhandle dates beyond 12/31/1999. On January 1st of the year 2000, will yourprograms think it is the year 2000, 1900, or even 1980? The goal of thisarticle is to ensure that your Visual Basic applications will behaveproperly with dates beyond 12/31/1999.
While all versions of Visual Basic will handle years greater than 1999 (ina four-digit format), passing a two-digit year in a date (such as 7/3/45)forces Visual Basic to assume what century the date corresponds to. Perhapsthe best way to explain this behavior would be to give a quick Visual Basichistory lesson.

Visual Basic History Lesson

For all versions of Visual Basic for Windows (including its predecessorssuch as Visual Basic for DOS and QuickBasic) prior to and including 3.0,two-digit years were always assumed to be in the 1900s. The code toimplement this default was built into each version's run-time library anddoes not depend on the version of the operating system or the century ofthe current system date.

Between the development cycles for Visual Basic 3.0 and 4.0, two newentities emerged: Visual Basic for Applications and OLE Automation. Priorto the advent of these technologies, Visual Basic's runtime librarycontained the code responsible for converting a two-digit year to afour-digit year. OLE Automation exposed a great deal of functionality thatother applications could access. Visual Basic for Applications did not needto implement this code; it could make calls to the OLE Automation librariesinstead.

Visual Basic 4.0 was developed with this interoperability in mind and beganto rely on the OLE Automation libraries to convert two-digit years tofour-digit years in most cases. The exception to the rule is the DateSerialfunction that was implemented in the Visual Basic runtime library becauseVisual Basic required more functionality than the OLE Automation librarycould provide at that time.

During the Visual Basic 4.0 development cycle, Microsoft decided that thedefaults used in previous versions of Visual Basic were reliable but notnecessarily practical. So, a new rule emerged. A two-digit year would beconverted to the current century of the system date. Thus,Year(Date("1/1/00")) would evaluate to the current century. This new rulewas implemented in the OLE Automation libraries used by Visual Basic 4.0and Visual Basic for Applications. The Visual Basic 4.0 runtime libraryalso implements the rule for the DateSerial function.

Microsoft later reconsidered and changed the default in the OLE Automation(now simply Automation) libraries as of version 2.20.4049 of OleAut32.dll.This change does not affect 16-bit applications that rely on the Automationlibraries, only 32-bit applications. Now, a two-digit year between 00 and29 (such as 17) is interpreted as 2017 while a two-digit year between 30and 99 (such as 72) is interpreted as 1972. The new Automation librariesprovide the functionality that Visual Basic requires for the DateSerialfunction. Thus, Visual Basic 5.0 and subsequent releases no longerimplement rules for this function in their runtime libraries.

The updated Automation library ships with Internet Explorer version 3.0 andlater, Windows NT 3.51 Service Pack 5, Windows NT 4.0, Windows 95 OSR 2,Office 97, Visual Basic 5.0 and other products.

What Does All of This Mean to Developers Who Use Visual Basic?

Visual Basic 3.0 and prior versions convert all two-digit years to the1900s.

Visual Basic 4.0 (16-bit) converts all two-digit years to the century ofthe current system date. Depending on the function used, Visual Basicconverts the date based on defaults in either the 16-bit Automationlibraries or the runtime library. The defaults in the 16-bit Automationlibraries have not been modified since Visual Basic 4.0 released so thebehavior is consistent regardless of which date function is used.

Visual Basic 4.0 (32-bit) converts two-digit years to four-digit yearsbased on the default in the Automation libraries except when using theDateSerial function that converts all two-digit years to the century of thecurrent system date. The 32-bit Automation libraries (OleAut32.dll version2.10) that shipped when Visual Basic 4.0 was released converted alltwo-digit years to the century of the current system date. Later 32-bitAutomation libraries (OleAut32.dll version 2.20 and later) converttwo-digit years to the 1900s if the two-digit year is between 30 and 99. Ifthe two-digit year is between 00 and 29, the date is converted to the2000s.

Visual Basic versions subsequent to 4.0, convert two-digit years to four-digit years based on the default in the Automation libraries for all datefunctions. Visual Basic 5.0 shipped with version 2.20.4054 that convertstwo-digit years to the 1900s if the two-digit year is between 30 and 99. Ifthe two-digit year is between 00 and 29, the date is converted to the2000s.

What If I Don't Like Those Defaults?

You may want to use your own set of rules instead of relying on thedefaults native to Visual Basic. For example, you may want to enter only atwo-digit year and have 00 to 49 correspond to the years 2000 to 2049 andhave 50 to 99 correspond to the years 1950 to 1999.

When accepting a date string from the user, test the format of the stringto determine the number of digits entered for the year. According to therules for this sample application, 1/11/45 is in the year 2045, and not inthe year 1945. Within the code for the application, change the string touse the appropriate four-digit year, then convert that date string with thefour-digit year into a date variable.

Sample Code

The following code evaluates the data entered into a textbox named txtDatewhen you click cmdConvertDate. If the date contains a two-digit year, thedate is converted into a four-digit year date according to the sample rule.The code then displays the initial date entered, the full year as convertedby the code according to the sample rule, and the full year is converted bythe defaults native to Visual Basic. Finally, the date displayed in txtDateis converted to a non-ambiguous date with the appropriate four-digit year.

Once you have developed code to handle your own rules for interpreting two-digit years, you can build that code into a 32-bit custom control with theControl Creation Edition of Visual Basic. For more information on thisproduct and on building your own custom controls, please see the MicrosoftVisual Basic Web site at:

This code requires that dates be entered in the mm/dd/yy format, but itcould easily be changed to handle a different date format:

   Private Sub cmdConvertDate_Click()       Dim strYear As String       Dim intSlash As Integer       If IsDate(txtDate) or txtDate = "2/29/00" Then           'Find first date separator.           intSlash = InStr(txtDate, "/")           If intSlash > 0 Then               'Find second date separator.               intSlash = InStr(intSlash + 1, txtDate, "/")               If intSlash > 0 Then                   'Extract the year from the date.                   strYear = Mid(txtDate, intSlash + 1)                   If Len(strYear) = 2 Then                       If CInt(strYear) < 50 Then                       ' Less than 50: year = 20XX.                           strYear = "20" & strYear                       Else                       ' Greater than 50: year = 19XX.                           strYear = "19" & strYear                       End If                   End If                   MsgBox "Date Entered: " & txtDate                   MsgBox "Year (Our Rule): " & strYear                   MsgBox "Year (VB Default): " & Year(txtDate)               Else                   MsgBox "Date not in expected format!"               End If           Else               MsgBox "Date not in expected format!"           End If       Else           MsgBox "Not a valid date!"       End If       '  Clarify date in txtDate.       txtDate.Text = Left(txtDate.Text, intSlash) & strYear   End Sub				
Microsoft Visual Basic Help file; Search on: Date and Variant Data Types;IsDate function; CDate and CVDate functions;

(c) Microsoft Corporation 1997, All Rights Reserved.Contributions by David Sceppa, Microsoft Corporation
IsDate Year 2000 CDate Date Y2K

Article ID: 162718 - Last Review: 02/28/2014 08:05:57 - Revision: 3.3

  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition
  • kbnosurvey kbarchive kbhowto KB162718