Calculate how many days (hours, seconds...) between 2 DateTime values

Published on Friday, April 8, 2011
c#

How to Calculate how many days (hours, seconds…) between 2 DateTime values?

Here we have example code:

We have 2 DateTimes: StartTime, EndTime

int idays = EndTime.Subtract(StartTime).Days;
int iHours = EndTime.Subtract(StartTime).Hours;
int iMinutesdays = EndTime.Subtract(StartTime).Minutes;
int iSeconds = EndTime.Subtract(StartTime).Seconds;