Export (0) Print
Expand All
11 out of 21 rated this helpful - Rate this topic

DateTimePicker Class

Updated: September 2010

Represents a Windows control that allows the user to select a date and a time and to display the date and time with a specified format.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[DefaultBindingPropertyAttribute("Value")]
[ComVisibleAttribute(true)]
public class DateTimePicker : Control

The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format. The DateTimePicker control makes it easy to work with dates and times because it handles a lot of the data validation automatically.

NoteNote:

The DateTimePicker control only supports Gregorian calendars.

When used to represent a date, the DateTimePicker control appears in two parts: a drop-down list with a date represented in text, and a calendar that appears when you click the down-arrow next to the list. The calendar looks like the MonthCalendar control, which can be used for selecting multiple dates. For more information about the MonthCalendar control, see MonthCalendar Control Overview (Windows Forms).

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

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

The Value property contains the current date and time the control is set to. You can use the Text property or the appropriate member of Value to get the date and time value. For more information, see How to: Set and Return Dates with the Windows Forms DateTimePicker Control. You can limit the dates and times that can be selected by setting the MinDate and MaxDate properties.

The values can be displayed in four formats, which are set by the Format property: Long, Short, Time, or Custom. The default date Format is DateTimePickerFormat.Long.

If you want the DateTimePicker to appear as a control for picking or editing times instead of dates, set the ShowUpDown property to true and the Format property to Time. For more information, see How to: Display Time with the DateTimePicker Control.

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, 2012 - Friday" by setting the CustomFormat property to "MMMM dd, yyyy - dddd". For more information, see How to: Display a Date in a Custom Format with the Windows Forms DateTimePicker Control and Custom Date and Time Format Strings.

Caution noteCaution:

When a DateTimePicker is data-bound and the backing value is changed to null, the value of the DateTimePicker will not be updated and the previous value will be retained. In situations where this behavior is not desirable (for example, when using a set of data-bound controls to page through a recordset) use the Format event of the Binding class to set the DateTimePicker to a value recognizable as a null.

The following code 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 a spin button control. To run this example, paste the following code into a form and call the CreateMyDateTimePicker method in the example form’s constructor or Load event handling method.

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;
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

Date

History

Reason

September 2010

Updated remarks. Added links to relevant topics.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.