What is the easist way to caclulate the difference in days between two dates?

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

I would prefer not to use a reference to VB... just want an elegant way to
calculate days between two dates.

For example, 2004/12/31 and 2005/01/01 would show an elapsed time in days of
1.
 
You could create a TimeSpan with one date taken away from the other (you
could use each ones Ticks property) and then the TimeSpan will give you the
difference in Days, Hours etc

Look up TimeSpan and you will find lots to fine tune what you need.
 
I would prefer not to use a reference to VB... just want an
elegant way to calculate days between two dates.

For example, 2004/12/31 and 2005/01/01 would show an elapsed
time in days of 1.

Ryan

DateTime start = new DateTime(2004, 12, 31);
DateTime end = new DateTime(2005, 1, 1);
TimeSpan ts = end - start;
int elapsedDays = ts.Days;
 
Back
Top