DateTimePicker: pick both date and time

前端 未结 7 1488
独厮守ぢ
独厮守ぢ 2020-11-28 04:01

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

相关标签:
7条回答
  • 2020-11-28 04:04

    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.

    0 讨论(0)
  • 2020-11-28 04:08

    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


    0 讨论(0)
  • 2020-11-28 04:12

    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.

    0 讨论(0)
  • 2020-11-28 04:18

    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 " ffffffffd, 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"

    0 讨论(0)
  • 2020-11-28 04:22

    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; 
    
    0 讨论(0)
  • 2020-11-28 04:26

    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.

    0 讨论(0)
提交回复
热议问题