Visual Studio .NET's DateTimePicker control offers a convenient way for users to make a date/time selection. The control allows you to select dates and times and to skip the date format validity of the data since the control lets you limit the input.

DateTimePicker is similar to the MonthView control, which allows you to select either a date or a range of dates but doesn't support time selection. In this quick tip, I will create an example that uses the DateTimePicker control.

Example

Find the DateTimePicker control in the Toolbox under the Common Controls section and add it to the Windows Form. Set the following properties:

|> Name: dtpDateSelection
|> CustomFormat: mm/dd/yyyy
|> Format: short
|> MaxDate: 12/31/2008
|> MinDate: 01/01/2007
|> ShowUpDown: False

Add the following code to the Form Load event:

dtpDateSelection.Value = Now

Create the following event:

Private Sub dtpDateSelection_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpDateSelection.CloseUp MessageBox.Show(dtpDateSelection.Value) End Sub

Result

In the first example, the ShowUpDown property is set to False, which means that the control's calendar portion is visible when the user clicks the control. The user then picks the date from the control's calendar portion. Select a date on the DateTimePicker control, and your screen will look similar to Figure A (http://www.builderau.com.au/builder/i/s/VB0726_FigA.gif).

Now change the ShowUpDown property value to True and run the code again. You will not see the control's calendar portion, and you will be able to pick a date by clicking the up/down arrows of the control. The result will look like Figure B (http://www.builderau.com.au/builder/i/s/VB0726_FigB.gif).

Notice that the control will not allow you to pick a date that's smaller than the MinDate property value or larger than the MaxDate property value.

For more information

There are many other properties and important methods you can use to get the DateTimePicker control to behave the way you want. MSDN offers additional details about how to use the DateTimePicker control.

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Related links

Comments

1

marwa - 12/12/07

how can i make datetimepicker make search to get data that i put it in the textbox

» Report offensive content

2

richter belmont - 26/01/08

how can i disable the textbox in the datetimepicker control so that the user can only use the calendar to input values not on the textbox?

» Report offensive content

3

Pralhad Sawant - 15/03/08

I want to store the value of datetimepicker control in database datetime datatype col. When i try then It rise the exception out of range

» Report offensive content

4

phillip - 10/04/08

How do calculate the number of days between 2 dateTimePickers

» Report offensive content

5

Mircea - 02/05/08

@phillip
Hello
You could calculate that number of days like this:
Dim x As Integer
x = DateTimePicker1.Value.Day - DateTimePicker2.Value.Day
MsgBox(x)

I think you`ll manage to make it work.
Bye

Dim x As Integer
x = DateTimePicker1.Value.Day - DateTimePicker2.Value.Day
MsgBox(x)

» Report offensive content

6

SasiKumar.K - 18/07/08

7

Rex - 29/08/08

You know when you drop down a datetimepicker a calendar shows up. At the top of the calendar Month and Year displays.
Does anybody know how to make Year invisible?
Thanks.

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

7

Rex - 29/08/08

You know when you drop down a datetimepicker a calendar shows up. At the top of the calendar Month and Year ... more

6

SasiKumar.K - 18/07/08

Nice ... more

5

Mircea - 05/02/08

@phillip Hello You could calculate that number of days like this: Dim x As Integer x = ... more

Log in


Sign up | Forgot your password?

  • Staff Apple to developer: Fart jokes aren't funny

    When Apple announced it would be vetting every application submitted for inclusion in the App Store, this was just the kind of question that entered many a mind: just how arbitrary would the company be in wielding that veto power? Read more »

    -- posted by Staff

  • Staff Chrome is just another browser

    Hands up if you missed the Chrome release -- didn't think anyone did. Google's browser arrived with all the fanfare and hype that only Google can produce. Read more »

    -- posted by Staff

  • Renai LeMay 2Vouch refers well

    Melbourne-based Web start-up 2Vouch yesterday launched the first public beta of what it dubs its "social recruiting platform". Read more »

    -- posted by Renai LeMay

What's on?