ARTICLE

DateTimePicker Control (Windows Forms) in VB.NET

Posted by Dinesh Beniwal Articles | Visual Basic 2010 September 21, 2009
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,
 
Reader Level:

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

  1. Set the Format property to DateTimePickerFormat.Custom.
  2. Set the CustomFormat property to a format string. For possible format characters, see DateTimePicker.CustomFormat Property.

            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)

Login to add your contents and source code to this article
share this article :
post comment
 

This is nice article For simplicity Related to this topic check this link http://www.mindstick.com/Articles/bccb2362-5980-408d-aae6-56bb67a9c7d4/?DateTimePicker Control in VB.Net

Posted by Gaurav Shukla Dec 15, 2010

how to calculate age? to using datetimepicker?

Posted by piragar janarthanan Jul 18, 2010

how to enable the date time picker control and many other controls  which are disabled in tollbox ..

Posted by joe christopher Mar 26, 2010

hello there,
                please,help me i want when the user select a date from datetimepicker the required information is displayed from sql server 2005 database in datagridview
this is the select satetment i write and the problem is on it.

SqlCommand cmd2 = new SqlCommand("select distinct EnrollNo,name1,datetime1,check1 from attendance where datetime1= '"+ dateTimePicker1.Value + "' order by datetime1 asc", c);

Posted by e a Dec 06, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor