문제

I have a DateTimePicker and a ComboBox. Im trying to combine the short Date of the DateTimePicker with the selected time of the combo box.

A few combobox selections are 12:00 AM 12:30 AM 1:00 AM etc.

So I have a DateTime from the datetime picker, how do I combine that with the combobox text to create a datetime class

도움이 되었습니까?

해결책

Use the DateTime.Parse() method:

var datePickerDate = dateTimePicker.Value;
var comboboxTime = comboBox.SelectedText;
var dateTimeString = String.Format("{0} {1}", datePickerDate, comboboxTime);

var combinedDate = DateTime.Parse(dateTimeString);

I'm not sure if you are writing a windows or web application, but the solutions are the same. Also you might want to use one of the overloaded versions of DateTime.Parse() to have more control of the format of the stringDateTime.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top