This documentation is archived and is not being maintained.

DateTimePicker.CustomFormat Property

Gets or sets the custom date/time format string.

[Visual Basic]
Public Property CustomFormat As String
[C#]
public string CustomFormat {get; set;}
[C++]
public: __property String* get_CustomFormat();
public: __property void set_CustomFormat(String*);
[JScript]
public function get CustomFormat() : String;
public function set CustomFormat(String);

Property Value

A string that represents the custom date/time format. The default is a null reference (Nothing in Visual Basic).

Remarks

To display string literals that contain date and time separators or format strings you must use escape characters in the substring. For example, to display the date as "June 06 at 3:00 PM", set the CustomFormat property to "MMMM dd 'at' t:mm tt". If the "at" substring is not enclosed by escape characters, the result is a "June 06 aP 3:00PM" because the "t" character is read as the one-letter AM/PM format string (see the format string table below).

The format strings can be combined to format the date and time. For example, to display the date and time as 06/01/2001 12:00 PM, this property should be set to "dd'/'MM'/'yyyy hh':'mm tt". For more information, see Date and Time Format Strings.

Note   The Format property must be set to DateTimePickerFormat.Custom for this property to affect the formatting of the displayed date/time.

The following table lists all the valid format strings and their descriptions.

Format String Description
d The one or two-digit day.
dd The two-digit day. Single digit day values are preceded by a zero.
ddd The three-character day-of-week abbreviation.
dddd The full day-of-week name.
h The one or two-digit hour in 12-hour format.
hh The two-digit hour in 12-hour format. Single digit values are preceded by a zero.
H The one or two-digit hour in 24-hour format.
HH The two-digit hour in 24-hour format. Single digit values are preceded by a zero.
m The one or two-digit minute.
mm The two-digit minute. Single digit values are preceded by a zero.
M The one or two-digit month number.
MM The two-digit month number. Single digit values are preceded by a zero.
MMM The three-character month abbreviation.
MMMM The full month name.
s The one or two-digit seconds.
ss The two-digit seconds. Single digit values are preceded by a zero.
t The one-letter AM/PM abbreviation ("AM" is displayed as "A").
tt The two-letter AM/PM abbreviation ("AM" is displayed as "AM").
y The one-digit year (2001 is displayed as "1").
yy The last two digits of the year (2001 is displayed as "01").
yyyy The full year (2001 is displayed as "2001").

Example

[Visual Basic, C#, C++] The following example sets the CustomFormat property so that the DateTimePicker will display the date as "June 01, 2001 - Friday". This code assumes that an instance of a DateTimePicker control has been created on a Form.

[Visual Basic] 
Public Sub SetMyCustomFormat()
    ' Set the Format type and the CustomFormat string.
    dateTimePicker1.Format = DateTimePickerFormat.Custom
    dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"
End Sub 'SetMyCustomFormat

[C#] 
public void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}


[C++] 
public:
void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1->Format = DateTimePickerFormat::Custom;
   dateTimePicker1->CustomFormat = S"MMMM dd, yyyy - dddd";
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DateTimePicker Class | DateTimePicker Members | System.Windows.Forms Namespace | DateTimePickerFormat | Format

Show: