DateTimePicker Control

The DateTimePicker control in Windows forms allows the user to choose one item from the dates or times list. If a date is displayed, this will appear in two parts: a drop-down list with a date in the text and a grid which appears when we click on the down arrow next to the list.

C# Date Time Picker Control

In other words, The DTP (DateTimePicker) controls provide a simple and intuitive interface through which date and time with the user can be exchanged. For instance, we can request the user to enter a date and then easily retrieve the selection using a DTP control.

 

If you want to see the DateTimePicker as a pick or edit times control instead of dates, set the ShowUpDown property to true and the Format property to time.

 

The check box will be displayed next to the selected control date, when the ShowCheckBox property is true.

The date-time value chosen can be updated when the check box is checked. The value is not available when the check box is empty.

C# Date Time Picker Control With Check Box

The MaxDate and MinDate control characteristics determine the dates and times range. The Value property includes the current date and time of the control.

Four formats are available, and these values are set by the format property: Long, Short, Time, and Custom.

The Value property returns the value of a DateTime structure. The DateTime structure contains several properties that return specific details about the date displayed. These properties can be used only to return a value; do not use it to define a value.

  • The date values return integer values for the time units of the selected dates for the month, day and year properties. A value for the selected day of the week is returned by the DayOfWeek property.
  • The time values return integer values for the time, minute, second, and millisecond properties for those time units.

Set the date and time value of the control

Set the Value property to a date or time value.


dateTimePicker1.Value = new DateTime(2000, 09, 11);
  

Call the Text property to return the whole value in the control, or call the property Value method to return a portion of the value.


MessageBox.Show ("The selected value is " +  dateTimePicker1.Text);  
MessageBox.Show ("The day of the week is " + dateTimePicker1.Value.DayOfWeek.ToString());  
MessageBox.Show("Millisecond is: " + dateTimePicker1.Value.Millisecond.ToString());

ToString can be used to transform information into a string which can be shown to the user.

 

We need to set the property CustomFormat to a suitable string if the custom format is selected.

  1. Set the Format property to DateTimePickerFormat.Custom.
  2. Set the CustomFormat property to a format string.


Example:


dateTimePicker1.Format = DateTimePickerFormat.Custom;  
// Display the date as "Nov 9 Feb 2000".  
dateTimePicker1.CustomFormat = "ddd dd MMM yyyy";

For example, the format string below shows the current date with a format "Today is: 05:30:31 Friday March 02, 2012" in the English (US) culture. The following format string is used for each character that is not a format character like "M", or an delimiter like":".

Example:


dateTimePicker1.CustomFormat = "'Today is:' hh:mm:ss dddd MMMM dd, yyyy";

Each character not included in single quotation marks may be changed, depending on the cultural setting. For example, the above format string shows the current date in English (U.S.) culture with the format "Today is: 05:30:31 Friday 02 March 2012."

Note that in a single quotation mark, the first colon should not be a delimiting character as it is in "hh: mm: ss."

The Table below describes different formats of date time and their results.

Specifier DateTimeFormatInfo property Results Pattern value (for en-US culture)
t ShortTimePattern h:mm tt "4:05 PM"
d ShortDatePattern M/d/yyyy "3/9/2008"
T LongTimePattern h:mm:ss tt "4:05:07 PM"
D LongDatePattern dddd, MMMM dd, yyyy "Sunday, March 09, 2008"
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt "Sunday, March 09, 2008 4:05 PM"
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt "Sunday, March 09, 2008 4:05:07 PM"
g (combination of d and t) M/d/yyyy h:mm tt "3/9/2008 4:05 PM"
G (combination of d and T) M/d/yyyy h:mm:ss tt "3/9/2008 4:05:07 PM"
m, M MonthDayPattern MMMM dd "March 09"
y, Y YearMonthPattern MMMM, yyyy "March, 2008"
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) "Sun, 09 Mar 2008 16:05:07 GMT"
s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) "2008-03-09T16:05:07"
u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) "2008-03-09 16:05:07Z"

Note: (*) = culture independent

 

Date Time Picker Source Code

Video Tutorial