Hello,
Considering values of DateTimePicker controls:
dtpDueDate.Value = 2014-03-25 15:39:12
dtpIssuedDate.Value = 2014-03-27 14:11:15
Substracting two DateTime objects will give you result in TimeSpan struct. If your date objects contains Time part you will get exact difference between two dates. In this case it will be:
1.22:32:03 (1 day, 22 hours, 32 minutes and 3 seconds)
Solution 1:
TimeSpan timeSpan = dtpDueDate.Value - dtpIssuedDate.Value;
txtNumOfDays.Text = timeSpan.Days.ToString();
txtNumOfDays.Text = timeSpan.TotalDays.ToString();
Solution 2:
If you wish to get only days difference without comparing TimePart use DateTime.Date:
TimeSpan timeSpan2 = dtpDueDate.Value.Date - dtpIssuedDate.Value.Date;
txtNumOfDays.Text = timeSpan2.TotalDays.ToString();
TimeSpan info:
http://msdn.microsoft.com/library/system.timespan%28v=vs.110%29.aspx[
^]
DateTime info:
http://msdn.microsoft.com/library/system.datetime.aspx[
^]
I hope it helps you :)