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