开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜