1

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!

Ivan K.
  • 13
  • 4

2 Answers2

1

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;
    }
Chris Schubert
  • 1,288
  • 8
  • 17
1

You can do

TimeSpan duration = endTime - startTime;

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

duration.TotalSeconds
duration.TotalMinutes
ry8806
  • 2,258
  • 1
  • 23
  • 32