I am trying to calculate how many days there are between different dates. Like the topic says, I'm getting a negative value. I guess I could just take the absolute value, but the fact that it returns a negative value, makes me doubt if the calculations are correct. Below is my code:
DateTime previousDay = new DateTime(1998, 6, 31, new GregorianCalender());
DateTime nextDay = new DateTime(1998, 6, 3, new GregorianCalender());
TimeSpan differenceInDays = (nextDay - previousDay);
double xAxisValue = differenceInDays.TotalDays;
Ofcourse 3-31 gives -28, but since we want it to show days between, it makes no sense that it doesn't return a positive value. Am I doing something wrong?
I also tried this approach:
(a - b).TotalDays
where a and b are of type Datime
Math.Abs
on the number and move on with your life ;-) – Adam Houldsworth Sep 26 '13 at 12:37Abs()
and everything will be fine. – Keith Payne Sep 26 '13 at 12:38.TotalDays
is returning the expected result. You are expecting it to return the difference in days, hence thedifferenceInDays
variable name, but that's just not what it calculates. – Henrique Miranda Sep 26 '13 at 12:49