I'm new to date timepickers. I was wondering if someone could explain them to me properly. I have a booking system that you must be able to book a date and specific time in the future and reserve that time frame in a sql database. Can I do it with date timepickers? It has a nice interface when its on my form but I cant see a way for the user to set the time? it always just gives me the default time. Any help? thanks
-
Can you give us more information? Win Forms? WPF? Version of .net? – Net Dev Oct 24 '13 at 15:34
-
possible duplicate of [DateTimePicker: pick both date and time](http://stackoverflow.com/questions/93472/datetimepicker-pick-both-date-and-time) – Daniel Abou Chleih Oct 24 '13 at 15:34
-
Show your code please. – tnw Oct 24 '13 at 15:36
-
dotnet framework 4. its a booking system to create a extraMathclass for people to go to. this is the admin who is creating the extraclass event with a class location, price, classtype(eg. grade 8 algebra extra class). what i want to use the datetimepicker for is sothat the admin can select a date and a time for the extra class – Andrea Visnenza Andy Oct 24 '13 at 15:42
2 Answers
Date Time pickers are pretty useful to use when you want people to be able to visually select a date/time. once the user selects a datetime, you can access it like this
int mymonth = this.datetimepicker1.Value.Month
int myday = this.datetimepicker1.Value.day
etc...
Furthermore, you can use events to trigger when stuff happens with them. So for instance, they select a date, you can use the ValueChanged event to make other fun stuff happen too.

- 118
- 1
- 10
-
thank you :) but how does the user pick the specific time in the datetimepicker – Andrea Visnenza Andy Oct 24 '13 at 15:43
-
added a picture of my form to the post. will that work? where i use your idea of adding a myday, mymonth and myyear and then getting the time manually with comboboxes? isnt there a built in way seeing that its called a dateTIMEpicker:P – Andrea Visnenza Andy Oct 24 '13 at 15:54
-
If you set the format to time, then they can choose the time by entering it in, and choose the date by clicking on the day picker icon like normal. dateTimePicker1.format = DateTimePickerFormat.Time – metinoheat Oct 24 '13 at 15:54
The DateTimePicker-Control has a Format-Property, which will set the format of the date and time displayed in the control. AS stated in this answer you can use following format to show Date and time:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM.dd.yyyy hh:mm:ss";
Here you can get a list of all format string and their descriptions: List of CustomFormat-String
The result will look like this:
.NET also has a structure called DateTime to save your selected value. You can save it with following code
DateTime yourSelectedDateTime = dateTimePicker1.Value;
textBox1.Text = yourSelectedDateTime.ToString();

- 1
- 1

- 2,440
- 2
- 19
- 31
-
thanks :) but what i want to know is there any way in the dateTimepicker to let the user physically define the selected time on the date? or must it be programmed manually into variables? – Andrea Visnenza Andy Oct 24 '13 at 15:56
-
Unfortunately you can only pick Dates with the DropDown of the DateTimePicker-Control. But you can raise and decrease every part of date and time with the arrow-keys or if you set the `ShowUpDown = true;` with two buttons (See here: http://www.dotnetperls.com/datetimepicker-2.png). But if you set ShowUpDown to true you'll have no DropDown-Menu anymore – Daniel Abou Chleih Oct 24 '13 at 16:06