[Solved] Cannot convert ‘string’ to ‘System.DateTime’ C# WPF


If MyClass expects a DateTime, you should use the value from the DatePicker‘s SelectedDate property. It returns a Nullable<DateTime> that can be converted to a DateTime using the GetValueOrDefault() method:

newObject = new Classes.MyClass(datePicker.SelectedDate.GetValueOrDefault());

If there is no date selected, GetValueOrDefault() will return DateTime.MinValue.

The DatePicker doesn’t store the date in any particular format. That’s a formatting thing.

It doesn’t set the time either. If you however set the time yourself, you could strip it away using the Date property of the Date time:

datePicker.SelectedDate.GetValueOrDefault().Date

5

solved Cannot convert ‘string’ to ‘System.DateTime’ C# WPF