Mohamed Ebrahim Mohamed Ebrahim - 1 year ago 59
C# Question

Difference between 2 dates showing wrong result

I'm trying to build a project and trying to compare between two dates from the date time picker.

Let's say it's departure and arrival time. The user chooses the date 5/11/2016 on departure and 6/11/2016 on arrival.

The code is below:

DateTime departure = dtpDeparture.Value.Date;
DateTime arrival = dtpArrival.Value.Date;

MessageBox.Show(departure- arrival).Days.ToString();

The result is: 6576 which is wrong.. it should be 1 (the difference is one day only)?

Thanks for the help.

Answer Source

(departure - arrival).TotalDays has always worked for me. Make sure your date format is correct too, you might be checking from May 11th to June 11th.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download