Editar

Partilhar via


DateTimePicker.CustomFormat Property

Definition

Gets or sets the custom date/time format string.

public:
 property System::String ^ CustomFormat { System::String ^ get(); void set(System::String ^ value); };
public string CustomFormat { get; set; }
public string? CustomFormat { get; set; }
member this.CustomFormat : string with get, set
Public Property CustomFormat As String

Property Value

A string that represents the custom date/time format. The default is null.

Examples

The following code 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.

public:
   void SetMyCustomFormat()
   {
      // Set the Format type and the CustomFormat string.
      dateTimePicker1->Format = DateTimePickerFormat::Custom;
      dateTimePicker1->CustomFormat = "MMMM dd, yyyy - dddd";
   }
public void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}
Public Sub SetMyCustomFormat()
    ' Set the Format type and the CustomFormat string.
    dateTimePicker1.Format = DateTimePickerFormat.Custom
    dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"
End Sub

Remarks

To display string literals that contain date and time separators or to format strings, you must use escape characters in the substring. For example, to display the date as "June 15 at 12: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 "June 15 aP 12:00PM" because the "t" character is read as the one-letter A.M./P.M. 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/15/2001 12:00 PM, this property should be set to "MM'/'dd'/'yyyy hh':'mm tt". For more information, see Custom 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 and 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 0.
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 0.
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 0.
m The one- or two-digit minute.
mm The two-digit minute. Single digit values are preceded by a 0.
M The one- or two-digit month number.
MM The two-digit month number. Single digit values are preceded by a 0.
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 0.
t The one-letter A.M./P.M. abbreviation (A.M. is displayed as "A").
tt The two-letter A.M./P.M. abbreviation (A.M. 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").

Applies to

See also