Im trying to calculate the how many days there are between two dates and display the result in a textblock, i am using wpf. However i get a nullable object must have a value
If you want to get the selected date, you're using the wrong property. DisplayDateStart
and DisplayDateEnd
limits the possible choices. They are nullable because it should be possible to create a date picker without any limitations. Change your start and finish assignments to use the DisplayDate
property instead:
DateTime start = datePicker1.DisplayDate.Date;
DateTime finish = datePicker2.DisplayDate.Date;
You should also consider renaming your datepickers. Having controls named datePicker1
and datePicker2
is typically hard to remember. What about datePickerStart
and datePickerEnd
?
(Disclaimer: I know that WPF controls typically don't get a name, because they are databound so the name won't matter, but in this case they are explicitly accessed and should have proper names).