Combining DatePicker and TimePicker

usually when I have timepicker and datePicker on winform I combine them into a single date object using the new DateTime constructor (y, m, d, h, mi, s). it seems rather long, and I was wondering what approach others use if it comes up against this situation.

DateTime date = 
new DateTime(DatePicker.Value.Year, DatePicker.Value.Month,  DatePicker.Value.Day, 
             TimePicker.Value.Hour, TimePicker.Value.Minute, TimePicker.Value.Second); 

enter image description here

looks like simple.

DateTime date  = DatePicker.Value.Date.Add(TimePicker.Value.TimeOfDay);

I did not realize that

+3
source share
2 answers

hope you used DateTimePicker to select date and time. in this case, you can combine the date and time, for example

DateTime date = DatePicker.Value.Date + TimePicker.Value.TimeOfDay;
+5
source

Have you tried DateTimePicker ?

+2
source

Source: https://habr.com/ru/post/1791318/


All Articles