PRB: Unexpected Date Value Returned from Format Function

Last reviewed: March 20, 1998
Article ID: Q113327
3.00 WINDOWS kbprg kbcode kbprb

The information in this article applies to:

- Microsoft Visual Basic programming system for Windows version 3.0

SYMPTOMS

When using the Date, Time, or Format function to return information about a Date/Time variable, you may get results other than what you expected. For Example, the following prints January:

   Print Format$(4, "MMMM") ' Print the Month represented by number 4

CAUSE

The value 4 when interpreted as a date is Jan 3, 1900. Dates are stored in Variants of VarType 7 (Date) as double-precision numbers. This number represents a date from January 1, 100 through December 31, 9999 -- where January 1, 1900 is 2. Negative numbers represent dates prior to December 30, 1899.

WORKAROUND

To get the string "April" to print when the only date information available is the Month 4, use a function such as DateSerial and supply it with an arbitrary Day and Year.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a command button (Command1) to the form.

  3. Add the following code to the Command1_Click event:

       Sub Command1_Click ()
    
          Dim AnyDay As Integer
          Dim AnyYear As Integer
    
          AnyDay = 1      ' Allowed values are: 1 to 31
          AnyYear = 1994  ' Allowed values are: 100 to 9999
    
          Print Format$(DateSerial(AnyYear, 4, AnyDay), "MMMM")
    
       End Sub
    
    

  4. Run the program.

This will print: April

STATUS

This behavior is by design.


Additional reference words: 3.00 FORMAT FORMAT$ DATESERIAL
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.