Microsoft KB Archive/180927

= FIX: Format Function Returns Incorrect Date or Number =

Article ID: 180927

Article Last Modified on 6/29/2004

-

APPLIES TO


 * 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 for Applications 5.0
 * Microsoft Visual Basic for Applications 6.0

-



This article was previously published under Q180927



SYMPTOMS
An incorrect date is returned from the Format function.



CAUSE
This problem can occur when the Grouping Symbol in the Regional Settings is set to a period (.).

For example, if your Regional Setting is "German(Standard)," the following line of code returns 5.30.1985 instead of 1.31.1997 as expected when typed into the debug window: ?Format("31.1.97", "m.d.yyyy") This problem is caused by the way the Format function handles the period character when formatting. In this case, the Format function simply ignores the period characters in the value before formatting occurs. In other words, Visual Basic converts the date 31.1.97 to the Long value 31197, so you receive the same result as if you typed: ?Format(31197, "m.d.yyyy")



RESOLUTION
This problem can be avoided by converting the date to be formatted to a Date type before it is passed to the Format function. For example, if you have this statement: ?Format("31.1.97", "m.d.yyyy") change it to: ?Format(CDate("31.1.97"), "m.d.yyyy") Because the Format function is capable of formatting several data types, it is a good idea to always pass the value to be formatted as the specific data type it represents to ensure there is no ambiguity.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been fixed in Visual Basic 6.0.



Steps to Reproduce Behavior
 Set the Regional Setting in Control Panel to "German(Standard)." NOTE: If you are running Windows 95, you will need to reboot after making this change. Create a new Standard EXE project. Form1 is created by default.  Add the following code to Form1: Private Sub Form_Click Debug.Print Format("31.1.97", "m.d.yyyy") End Sub  Run the project and click the form. Note that the incorrect value of "5.30.1985" is displayed in the debug window.

Additional query words: kbVBp500bug kbVBp600fix kbVBA500bug kbVBA600bug KBVBA KBINTL kbVBp kbdsd kbDSupport

Keywords: kbbug kbfix KB180927

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.