tags:

views:

2302

answers:

4

I see that this question has been answered for Java, Javascript, and PHP, but not C#. So, how might one calculate the number of days between two dates in C#?

+8  A: 

Assuming a and b are of type DateTime:

(a - b).TotalDays
Greg Beech
+2  A: 

Use TimeSpan object which is the result of date substraction:

DateTime d1;
DateTime d2;
return (d1 - d2).TotalDays;
Vitaliy Liptchinsky
+1  A: 

I think this will do what you want:

DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);

TimeSpan t = d1 - d2;
double NrOfDays = t.TotalDays;
Cloud
+1  A: 
     DateTime xmas = new DateTime(2009, 12, 25);
 double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;
Philip Wallace