160

Is it possible to use DateTimePicker (Winforms) to pick both date and time (in the dropdown)? How do you change the custom display of the picked value? Also, is it possible to enable the user to type the date/time manually?

Gary Barrett
  • 1,764
  • 5
  • 21
  • 33
Grzenio
  • 35,875
  • 47
  • 158
  • 240

8 Answers8

208

Set the Format to Custom and then specify the format:

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss";  

or however you want to lay it out. You could then type in directly the date/time. If you use MMM, you'll need to use the numeric value for the month for entry, unless you write some code yourself for that (e.g., 5 results in May)

Don't know about the picker for date and time together. Sounds like a custom control to me.

Ahmad
  • 5,551
  • 8
  • 41
  • 57
itsmatt
  • 31,265
  • 10
  • 100
  • 164
  • 20
    24 hour clock: "dd MM yyyy HH mm ss". I guess developer has to care about localization (different date/time format in different countries). Greetings from Austria. – hfrmobile Sep 30 '13 at 08:39
  • 9
    @hfrmobile - you can also use `System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern` and `System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern` to get the format for the current culture. [MSDN link](https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo) – Wai Ha Lee Jan 26 '15 at 15:14
  • 1
    Agree with @WaiHaLee: `Dim date As String = CultureInfo...ShortDatePattern Dim time As String = CultureInfo...ShortTimePattern dtpThing.Format = DateTimePickerFormat.Custom dtpThingEnd.CustomFormat = date + " " + time` You also can constrain, say, the time component manually: `Dim date As String = CultureInfo...ShortDatePattern Dim time As String = CultureInfo...ShortTimePattern dtpThing.Format = DateTimePickerFormat.Custom dtpThingEnd.CustomFormat = date + " HH:mm"` – SteveCinq Jan 31 '17 at 23:52
  • 1
    I used this but I put the CustomFormat in the picker properties rather than using code – blind Skwirl Oct 22 '19 at 21:48
86

It is best to use two DateTimePickers for the Job One will be the default for the date section and the second DateTimePicker is for the time portion. Format the second DateTimePicker as follows.

      timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
      timePortionDateTimePicker.ShowUpDown = true;

The Two should look like this after you capture them

Two Date Time Pickers

To get the DateTime from both these controls use the following code

DateTime myDate = datePortionDateTimePicker.Value.Date + 
                    timePortionDateTimePicker.Value.TimeOfDay; 

To assign the DateTime to both these controls use the following code

datePortionDateTimePicker.Value  = myDate.Date;  
timePortionDateTimePicker.Value  = myDate.TimeOfDay; 
Vectoria
  • 1,627
  • 14
  • 13
46

Unfortunately, this is one of the many misnomers in the framework, or at best a violation of SRP.

To use the DateTimePicker for times, set the Format property to either Time or Custom (Use Custom if you want to control the format of the time using the CustomFormat property). Then set the ShowUpDown property to true.

Although a user may set the date and time together manually, they cannot use the GUI to set both.

Nescio
  • 27,645
  • 10
  • 53
  • 72
26

DateTime Picker can be used to pick both date and time that is why it is called 'Date and Time Picker'. You can set the "Format" property to "Custom" and set combination of different format specifiers to represent/pick date/time in different formats in the "Custom Format" property. However if you want to change Date, then the pop-up calendar can be used whereas in case of Time selection (in the same control you are bound to use up/down keys to change values.

For example a custom format " ddddd, MMMM dd, yyyy hh:mm:ss tt " will give you a result like this : "Thursday, August 20, 2009 02:55:23 PM".

You can play around with different combinations for format specifiers to suit your need e.g MMMM will give "August" whereas MM will give "Aug"

Danish
  • 694
  • 1
  • 7
  • 15
  • 3
    "DateTime Picker can be used to pick both date and time that is why it is called 'Date and Time Picker'." - except that it can't, which is why the question was asked. – Derf Skren Feb 19 '15 at 03:31
  • 1
    Indeed, this control is very frustrating. I just spent an afternoon debugging code until I realized that the darn thing will return a value that represents the user-edited date **OR** time, but not both. I have to vent: how can a control show a valid user-edited date and time (my custom format is `yyyy-MM-dd HH:mm`) and then return a value that reflects only part of the **displayed** date and time. I'd call it a bug, but it's been there for decades... – EBlake Jul 21 '17 at 21:39
20

Go to the Properties of your dateTimePickerin Visual Studio and set Format to Custom. Under CustomFormat enter your format. In my case I used MMMMdd, yyyy | hh:mm

enter image description here
dateTimePickerProperties

Serge V.
  • 3,377
  • 3
  • 20
  • 28
  • 2
    He is not asking how to format/display the time on the DateTimePicker. He is asking how to make it pick a time (hence the name DateTimePicker, one would assume that the control could also be used to pick a time) – nivs1978 May 16 '19 at 13:07
  • 1
    This should be the right answer. Worked like a charm – KingMak Mar 13 '20 at 18:50
1

You can get it to display time. From that you will probably have to have two controls (one date, one time) the accomplish what you want.

Craig
  • 11,614
  • 13
  • 44
  • 62
1

I'm afraid the DateTimePicker control doesn't have the ability to do those things. It's a pretty basic (and frustrating!) control. Your best option may be to find a third-party control that does what you want.

For the option of typing the date and time manually, you could build a custom component with a TextBox/DateTimePicker combination to accomplish this, and it might work reasonably well, if third-party controls are not an option.

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
0

If you need (24 hours) military time. You should use "HH" instead of "hh".

"MM/dd/yyyy HH:mm"