DateTimePicker Control (Windows Forms) in VB.NET
The Windows Forms DateTimePicker control allows the user to select a single item from a list of dates or times. When used to represent a date,
Introduction to the Windows Forms DateTimePicker Control
The Windows Forms DateTimePicker control allows the user to select a single item from a list of dates or times. When used to represent a date, it appears in two parts: a drop-down list with a date represented in text, and a grid that appears when you click on the down-arrow next to the list. The grid looks like the MonthCalendar control, which can be used for selecting multiple dates. For more information on the MonthCalendar control, see Introduction to the Windows Forms MonthCalendar Control. An alternative to the grid, useful for editing times instead of dates, is the up and down buttons that appear when the ShowUpDown property is set to true.
When the ShowCheckBox property is set to true, a check box is displayed next to the selected date in the control. When the check box is checked, the selected date-time value can be updated. When the check box is empty, the value appears unavailable.
The control's MaxDate and MinDate properties determine the range of dates and times. The Value property contains the current date and time the control is set to. For details, see Setting and Returning Dates with the Windows Forms DateTimePicker Control. The values can be displayed in four formats, which are set by the Format property: Long, Short, Time, or Custom. If a custom format is selected, you must set the CustomFormat property to an appropriate string. For details, see Displaying a Date in a Custom Format with the Windows Forms DateTimePicker Control.
Displaying 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 Enumeration. If none of these is adequate for your purposes, you can create your own format style using format characters listed in DateTimePicker.CustomFormat Property.
To display a custom format
Set the Format property to
DateTimePicker1.Format = DateTimePickerFormat.Custom
' Display the date as "Mon 26 Feb 2001".
DateTimePicker1.CustomFormat = "ddd dd MMM yyyy"
Adding Text to the Formatted Value
You can add text to the formatted string. In general, you should use single quotes to enclose any character that is not a format character like "M" or a delimiter like ":". Depending on the culture setting, any characters not enclosed in single quotes may be changed. 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. Note that the first colon is enclosed in single quotes, 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".
DateTimePicker1.CustomFormat = "'Today is:' hh:mm:ss dddd MMMM dd, yyyy"
Setting and Returning Dates with the Windows Forms DateTimePicker Control
The currently selected date or time in the Windows Forms DateTimePicker control is determined by the Value property. You can set the Value property before the control is displayed (for example, at design time or in the Form_Load event) to determine which date will be initially selected in the control. By default, the control's Value is set to the current date. If you change the control's Value in code, the control is automatically updated on the form to reflect the new setting.
The Value property returns a DateTime structure as its value. There are several properties of the DateTime structure that return specific information about the displayed date. These properties can only be used to return a value; do not use them to set a value.
- For date values, the Month, Day, and Year properties return integer values for those time units of the selected date. The DayOfWeek property returns a value indicating the selected day of the week (possible values are listed in DayOfWeek Enumeration).
- For time values, the Hour, Minute, Second, and Millisecond properties return integer values for those time units.
To set the date and time value of the control
Set the Value property to a date or time value.
DateTimePicker1.Value = New DateTime(2001, 10, 20)
To return the date and time value
Call the Text property to return the entire value as formatted in the control, or call the appropriate method of the Value property to return a part of the value. Use ToString to convert the information into a string that can be displayed to the user.
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)