Search code examples
c#wpfdatetimedatetimepickerwpf-extended-toolkit

How to find a difference between two dates from WPF Extended Toolkit?


I am using DateTimePicker from WPF Extended Tooklit(http://wpftoolkit.codeplex.com/wikipage?title=DateTimePicker&referringTitle=Documentation)

So, I have two values: Start Time and End Time (both are DateTimePickers from WPF Extended Toolkit), how can I find difference between these values? Also, I want to find difference between Start Time and DateTime.Now to print how much time left.

Thanks in advance.


DateTime? firstDate = datetimepicker1.Value;
DateTime? secondDate = datetimepicker2.Value;
TimeSpan? duration = firstDate - secondDate;
string d = duration.ToString();

That worked for me. Thank you, ry8806 and Chris Schubert!


Solution

  • You can use the DateTime's subtract method. It will return a TimeSpan which has the duration properties you're looking for.

    DateTime? firstDate = picker.SelectedDate;
    DateTime? secondDate = picker2.SelectedDate;
    
        if (firstDate!= null && secondDate != null)
        {
            TimeSpan duration = firstDate.Subtract(secondDate);
            return duration.TotalDays;
        }