Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/display-a-date-in-a-custom-format-with-wf-datetimepicker-control.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

3.3 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
Display a Date in a Custom Format with DateTimePicker Control Learn how to use the Windows Forms DateTimePicker control to format the display of dates and times in the control. 03/30/2017
csharp
vb
cpp
DateTimePicker control [Windows Forms], display styles
examples [Windows Forms], DateTimePicker control
dates [Windows Forms], displaying in DateTimePicker control
39767691-2d2b-46b6-a663-b7901e581a6e

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

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

To display a custom format

  1. Set the xref:System.Windows.Forms.DateTimePicker.Format%2A property to DateTimePickerFormat.Custom.

  2. Set the xref:System.Windows.Forms.DateTimePicker.CustomFormat%2A property to a format string.

    DateTimePicker1.Format = DateTimePickerFormat.Custom  
    ' Display the date as "Mon 27 Feb 2012".  
    DateTimePicker1.CustomFormat = "ddd dd MMM yyyy"  
    
    dateTimePicker1.Format = DateTimePickerFormat.Custom;  
    // Display the date as "Mon 27 Feb 2012".  
    dateTimePicker1.CustomFormat = "ddd dd MMM yyyy";  
    
    dateTimePicker1->Format = DateTimePickerFormat::Custom;  
    // Display the date as "Mon 27 Feb 2012".  
    dateTimePicker1->CustomFormat = "ddd dd MMM yyyy";  
    

To add text to the formatted value

  1. 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 02, 2012" 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->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 02, 2012" 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 02, 2012".

See also