Microsoft KB Archive/186424

= INFO: DateTimePicker CustomFormat Is Case-Sensitive =

Article ID: 186424

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q186424



SUMMARY
The CustomFormat property of the DateTimePicker control is case-sensitive.



MORE INFORMATION
As an example, if you use a CustomFormat such as mm/dd/yy, you will get the correct day and year but not the month. This is because the symbol "mm" refers to minutes instead of months. Because all dates without a time-specified default to midnight, the hours, minutes, and seconds will all have a value of zero.

Therefore, you might get:   00/29/98 instead of:   04/29/98 as you might expect. To specify a month in the CustomFormat, use a capital letter "M."

Similarly, to see time on a 12-hour clock, use a lower case "h" for hours. Or use upper-case "H" for hours to view time on a 24-hour clock.

If you want to include any literal text in your custom format, it should be enclosed in single quotes. While many characters will display correctly without using single quotes, such as spaces, slashes, digits, etc., the quotes are recommended to ensure a proper format. If you want to have a single quote display in your format, use two single quotes together. Also, only three 'y' characters are required for a four-digit year, but it is also fine to use four or more 'y' characters for year because the extras are ignored.

The CustomFormat property is a String. So, when setting it in code, the entire custom format must be enclosed in a pair of double quotes. For example: DTPicker1.CustomFormat = "'Today''s date is' MMM d, yyy"

Step-by-Step Example
 Create a new Standard EXE project. Form1 is created by default. Select Components from the Project menu, check "Microsoft Windows Common Controls-2 6.0," and then click OK. Add a DTPicker control to Form1. Set the control's Format property to 3-dtpCustom. Enter a custom format into the control's CustomFormat property.  Add the following code to the Form's module: Private Sub Form_Load DTPicker1.Value = "May 6, 1998 23:05:30" End Sub  Run the project and notice the format of the date.</li>  Repeat steps 4 and 5 using different custom formats.

The following table describes the results for some sample CustomFormats: <pre class="fixed_text">  CustomFormat              Display ---

mm/dd/yy                 00/06/98 MM'/'d'/'yyy             05/6/1998 MM/dd/yyyyyy             05/06/1998 MM-DD-YY                 05-DD-YY 'Today is 'MMM d, yyy    Today is May 6, 1998 MMM yy               May '98  (pair of single quotes within                                      single quotes) hh:mm tt                 11:05 PM   HH:mm:ss                  23:05:30 MMMM d h:m tt            May 5 11: 5 PM                        </li></ol>

<div class="references_section">