DateTimePicker: pick both date and time

前端 未结 7 1560
独厮守ぢ
独厮守ぢ 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: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; 
    

提交回复
热议问题