Export (0) Print
Expand All

DateTimePicker Class

Represents a Windows date/time picker control.

For a list of all members of this type, see DateTimePicker Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.DateTimePicker

[Visual Basic]
Public Class DateTimePicker
   Inherits Control
[C#]
public class DateTimePicker : Control
[C++]
public __gc class DateTimePicker : public Control
[JScript]
public class DateTimePicker extends Control

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The DateTimePicker control is used to allow the user to select a date and time, and to display that date/time in the specified format. You can limit the dates and times that can be selected by setting the MinDate and MaxDate properties.

You can change the look of the calendar portion of the control by setting the CalendarForeColor, CalendarFont, CalendarTitleBackColor, CalendarTitleForeColor, CalendarTrailingForeColor, and CalendarMonthBackground properties.

The Format property sets the DateTimePickerFormat of the control. The default date Format is DateTimePickerFormat.Long. If the Format property is set to DateTimePickerFormat.Custom, you can create your own format style by setting the CustomFormat property and building a custom format string. The custom format string can be a combination of custom field characters and other literal characters. For example, you can display the date as "June 01, 2001 - Friday" by setting the CustomFormat property to "MMMM dd, yyyy - dddd". For more information, see Date and Time Format Strings.

To use an up-down style control to adjust the date/time value, set the ShowUpDown property to true. The calendar control will not drop down when the control is selected. The date/time can be adjusted by selecting each element individually and using the up and down buttons to change the value.

You might consider using a DateTimePicker control instead of a MonthCalendar if you have a need for custom date formatting and limiting a selection to just one date. Using the DateTimePicker will limit the need for much data validation of date/time values.

Note   The DateTimePicker control only supports Gregorian calendars.

Example

[Visual Basic, C#, C++] The following example creates an new instance of a DateTimePicker control and initializes it. The control's CustomFormat property is set. Also, the ShowCheckBox property is set so that the control displays a CheckBox, and the ShowUpDown property is set so that the control is displayed as an up-down control.

[Visual Basic] 
Public Sub CreateMyDateTimePicker()
    ' Create a new DateTimePicker control and initialize it.
    Dim dateTimePicker1 As New DateTimePicker()
    
    ' Set the MinDate and MaxDate.
    dateTimePicker1.MinDate = New DateTime(1985, 6, 20)
    dateTimePicker1.MaxDate = DateTime.Today
    
    ' Set the CustomFormat string.
    dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"
    dateTimePicker1.Format = DateTimePickerFormat.Custom
    
    ' Show the CheckBox and display the control as an up-down control.
    dateTimePicker1.ShowCheckBox = True
    dateTimePicker1.ShowUpDown = True
End Sub 'CreateMyDateTimePicker

[C#] 
public void CreateMyDateTimePicker()
{
   // Create a new DateTimePicker control and initialize it.
   DateTimePicker dateTimePicker1 = new DateTimePicker();

   // Set the MinDate and MaxDate.
   dateTimePicker1.MinDate = new DateTime(1985, 6, 20);
   dateTimePicker1.MaxDate = DateTime.Today;

   // Set the CustomFormat string.
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
   dateTimePicker1.Format = DateTimePickerFormat.Custom;

   // Show the CheckBox and display the control as an up-down control.
   dateTimePicker1.ShowCheckBox = true;
   dateTimePicker1.ShowUpDown = true;
}
   

[C++] 
public:
void CreateMyDateTimePicker()
{
   // Create a new DateTimePicker control and initialize it.
   DateTimePicker* dateTimePicker1 = new DateTimePicker();

   // Set the MinDate and MaxDate.
   dateTimePicker1->MinDate = DateTime(1985, 6, 20);
   dateTimePicker1->MaxDate = DateTime::Today;

   // Set the CustomFormat string.
   dateTimePicker1->CustomFormat = S"MMMM dd, yyyy - dddd";
   dateTimePicker1->Format = DateTimePickerFormat::Custom;

   // Show the CheckBox and display the control as an up-down control.
   dateTimePicker1->ShowCheckBox = true;
   dateTimePicker1->ShowUpDown = true;
}
   

[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

Namespace: System.Windows.Forms

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

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

DateTimePicker Members | System.Windows.Forms Namespace | Size

Show:
© 2014 Microsoft