Working with DatePicker in WPF : Subtraction, Addition problem
Have Two DatePicker Controls and one button on my WPF page. The Click event of button shows the number of days between datePicker2 and datePicker1.
I am trying to use the following code, but code couldn't compile and gives the System.Nullable' does not contain a definition for 'Subtract' and no extension method 'Subtract' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?) Error
Here's the开发者_如何学JAVA Code :
private void button1_Click(object sender, RoutedEventArgs e)
{
TimeSpan diff1 = datePicker1.SelectedDate.Subtract(datePicker1.SelectedDate);
i = diff1.TotalDays;
MessageBox.Show(i.ToString(), "Show");
}
I tried using DisplayDate in place of SelectedDate but that code always output 0.
DatePicker.SelectedDate
returns a Nullable, which indeed has no method Substract
. You can simply do
TimeSpan diff1 = datePicker2.SelectedDate - datePicker1.SelectedDate;
Another option would be to cast both nullable types of SelectedDate
to normal DateTime
.
精彩评论