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?

Offensive content reported

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

Offensive content reported

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)

Offensive content reported

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

8

anand - 15/09/08

How can i store the value from datetimepicker in to the database in ms-access.

Offensive content reported

9

tony - 26/09/08

How do you select the time.
In none of your examples to you show how to select the time.

» Report offensive content

10

Esaau - 02/10/08

Hi everyone.
Can please someone help me here. I want to write a code that writes from the textbox to the datetimepicker.

For instance, when I write a date of birth from the textbox, that value may be populated in the datetimepicker control

» Report offensive content

11

Kingsley - 10/10/08

Hi gang,
how can i display in the dropdown calender of the datetimepicker only the weekdays or maybe make the weekend days unclickerble

Offensive content reported

12

FelixNicol - 23/10/08

Ty, that was just the event I needed for validating a period made of 2 dates (begin and end)!

Private Sub DateBegin_CloseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateBegin.CloseUp
If Me.DateBegin.Value > Me.DateEnd.Value Then
Me.DateEnd.Value = Me.DateBegin.Value
End If
End Sub

Private Sub DateEnd_CloseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateEnd.CloseUp
If Me.DateEnd.Value < Me.DateBegin.Value Then
Me.DateBegin.Value = Me.DateEnd.Value
End If
End Sub

» Report offensive content

13

jigisha - 18/04/09

how to put datetime picker value in crytal report

» Report offensive content

14

chinna - 10/08/09

about addition of date

i am having two textbox  in one textbox i gave current  date and in another textbox i gave one date .if i have to check current date minus the date  i entered is 90 days difference
current date -anotherdate=90days what will be the code

» Report offensive content

15

Lester Marquez - 29/08/09

how can I remove the time on your codings

your code response this output:
ex: 08/08/2009 11:30am
but, how can I Remove the time?
I am Using Label into my output
I just need the date...

thanks...

|> 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

this is your code right?

» Report offensive content

16

tani - 03/09/09

i m not getting the time...in datetimepicker..m only getting the date..wat will b the property change or coding..plz help..using visual studio 2005

» Report offensive content

17

SITHARTHAN MANI - 18/09/09

I have clicked datetimepicker object, popup shown. Meanwhile, i have close that particular form, but popup was not closed that time. Stil it is shown.
When i go to next step, i am getting access violation error.

Anybody can help me, to close datetimepicker instance when i close that particular form

» Report offensive content

18

atin - 24/09/09

how can i, with the help of datetime picker get the time up to date

Offensive content reported

19

Arun - 12/10/09

all controls code in c#

» Report offensive content

20

Aaron - 21/11/09

I have a program that records dates to an access database. I am trying to search through those records using 2 datetimepicker controsl, by checking to see if the database records are <, > the datetimepicker controls. The problem is wierd, when the computer sorts through the dates it goes through a numberical sort order, and not by actual date, such as 1,10,11,12,13,14....18,19,2,20,21,21...etc

I have the date formatted everywhere as custom,(dd/MM/yyyy).

How can I get it to add day by day, from the first to the second and then the third, and so on?

If DepositDateDateTimePicker.Text >= DateTimePicker1.Text And DepositDateDateTimePicker.Text <= DateTimePicker2.Text Then

» Report offensive content

21

ct virus - 09/12/09

how to compare to two year in date time picker control in vb.net

if DTp_1.year != DTp_2.year

'DTp is datetimepicker

it is possible to compare it

» Report offensive content

22

Hav0c - 28/01/10

Hey
I have a date “2010/01/27” (yyyy/mm/dd) in my Access database stored as a sting now I know how to get it from the database using a OleDbDataReader now the question is How do I display that date “2010/01/27” in my DateTimePicker as “12 January 2010” ??

» Report offensive content

23

Hav0c - 28/01/10

Hey
I am storing the date that I get from a DateTimePicker as a string in my ms access database
dtpDOB is my DateTimePicker variable name

Dim dob As String
dob = dtpDOB.Value.Year.ToString + "/" + dtpDOB.Value.Month.ToString + "/" + dtpDOB.Value.Day.ToString
‘yyyy/mm/dd

query = "INSERT INTO YOURTABLENAME VALUES ('" + dob +"')"

» Report offensive content

24

puja - 03/05/10

How can i assign a value to the datetimepicker?the value of date in datetimepicker should be 1 month prior to current date

» Report offensive content

25

Nilesh Vat - 25/05/10

I want to see the birthday on today in a school management software in vb.net. For this i need only to compare the date and month only not year. So how can i do this pls help me in this case.

» Report offensive content

26

felix beto - 03/07/10

como podemos realiar la codificacion con el datetimepicker

» Report offensive content

Leave a comment

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

* indicates mandatory fields.

26

felix beto - 07/03/10

como podemos realiar la codificacion con el datetimepicker ... more

25

Nilesh Vat - 25/05/10

I want to see the birthday on today in a school management software in vb.net. For this i need only to ... more

24

puja - 05/03/10

How can i assign a value to the datetimepicker?the value of date in datetimepicker should be 1 month prior to current ... more

Log in


Sign up | Forgot your password?

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!