다음을 통해 공유


How to: Display a Date in a Custom Format with the Windows Forms DateTimePicker Control

The Windows Forms DateTimePicker control gives you flexibility in formatting the display of dates and times in the control. The Format property allows you to select from predefined formats, listed in the DateTimePickerFormat. If none of these is adequate for your purposes, you can create your own format style using format characters listed in CustomFormat.

To display a custom format

  1. Set the Format property to DateTimePickerFormat.Custom.

  2. Set the CustomFormat property to a format string.

    DateTimePicker1.Format = DateTimePickerFormat.Custom
    ' Display the date as "Mon 26 Feb 2001".
    DateTimePicker1.CustomFormat = "ddd dd MMM yyyy"
    
    dateTimePicker1.Format = DateTimePickerFormat.Custom;
    // Display the date as "Mon 26 Feb 2001".
    dateTimePicker1.CustomFormat = "ddd dd MMM yyyy";
    
    // Display the date as "Mon 26 Feb 2001".
    dateTimePicker1.set_CustomFormat("ddd dd MMM yyyy");
    
    dateTimePicker1->Format = DateTimePickerFormat::Custom;
    // Display the date as "Mon 26 Feb 2001".
    dateTimePicker1->CustomFormat = "ddd dd MMM yyyy";
    

To add text to the formatted value

  • Use single quotation marks to enclose any character that is not a format character like "M" or a delimiter like ":". For example, the format string below displays the current date with the format "Today is: 05:30:31 Friday March 03, 2001" in the English (United States) culture.

    DateTimePicker1.CustomFormat = "'Today is:' hh:mm:ss dddd MMMM dd, yyyy"
    
    dateTimePicker1.CustomFormat = "'Today is:' hh:mm:ss dddd MMMM dd, yyyy";
    
    dateTimePicker1.set_CustomFormat("'Today is:' hh:mm:ss dddd MMMM dd, yyyy");
    
    dateTimePicker1->CustomFormat =
       "'Today is:' hh:mm:ss dddd MMMM dd, yyyy";
    

    Depending on the culture setting, any characters not enclosed in single quotation marks may be changed. For example, the format string above displays the current date with the format "Today is: 05:30:31 Friday March 03, 2001" in the English (United States) culture. Note that the first colon is enclosed in single quotation marks, because it is not intended to be a delimiting character as it is in "hh:mm:ss". In another culture, the format might appear as "Today is: 05.30.31 Friday March 03, 2001".

See Also

Tasks

How to: Set and Return Dates with the Windows Forms DateTimePicker Control

Other Resources

DateTimePicker Control (Windows Forms)