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

1.3k Views Asked by At

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!

2

There are 2 best solutions below

0
On BEST ANSWER

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;
    }
0
On

You can do

TimeSpan duration = endTime - startTime;

Then on the Timespan (duration) you can access many properties e.g.:

duration.TotalSeconds
duration.TotalMinutes