DateTime Picker In WinForm How To Pick Time? [dupl

2019-02-11 12:34发布

Possible Duplicate:
DateTimePicker: pick both date and time

I'm using a WinForm DateTime picker and it does a fantastic job of choosing dates, but I don't know how to do times. It looks like a time is associated with the value, so maybe there's a way to use this to pick a time?

If there is no built in way to do this, I'll just create another box for the time and modify the DateTime value from the DateTime picker.

Thanks!

3条回答
女痞
2楼-- · 2019-02-11 13:08

You can use the built in DateTime picker by adding a custom format string as follows:

DateTimePicker.ShowUpDown = true;
DateTimePicker.CustomFormat = "hh:mm";
DateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;

Enjoy!

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-11 13:11

You can either choose the datepicker to have a "long" date, even only "time" or you can create your custom date.

I always use this format, as it's the most easy one to understand for users (IMHO): yyyy.MM.dd HH:mm

This can be done in the designer the fastest, just change the property.

Or, change it in the program with

YourDatePicker.Format = DateTimePickerFormat.Custom;
YourDatePicker.CustomFormat = "yyyy.MM.dd HH:mm";
查看更多
ら.Afraid
4楼-- · 2019-02-11 13:12

The DateTimePicker works pretty much like how setting the Windows clock works. If you set the ShowUpDown property to true, it displays a spin control to the right of the DateTimePicker. If you then click on a section of the control, such as the time in hours, and then hit the up or down arrow of the spin control, it will change the time in hours.

Also, if you want to use a custom DateTime format, change the Format property to Custom and set the flags you'd like. For example, MM ffffdd yyyy HH:mm:ss. For an explanation of all the custom format specifiers, here's the full list of them from MSDN.

Hope that helps.

查看更多
登录 后发表回答