Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2010
Visual Studio
Windows Forms
 How to: Set and Return Dates with t...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework 4 - Windows Forms
How to: Set and Return 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's 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 the 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.

    Visual Basic
    DateTimePicker1.Value = New DateTime(2001, 10, 20)

    C#
    dateTimePicker1.Value = new DateTime(2001, 10, 20);

    Visual C++
    dateTimePicker1->Value = 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.

    Visual Basic
    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)

    C#
    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());

    Visual C++
    MessageBox::Show (String::Concat("The selected value is ",
       dateTimePicker1->Text));
    MessageBox::Show (String::Concat("The day of the week is ",
       dateTimePicker1->Value.DayOfWeek.ToString()));
    MessageBox::Show(String::Concat("Millisecond is: ",
       dateTimePicker1->Value.Millisecond.ToString()));
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker