Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top