How can I calculate the difference between 2 DateTimes
?
DateTime a = DateTime.Now;
DateTime b = DateTime.Now.AddHours(1);
int differenceInMilliseconds;
How can I calculate the difference between 2 DateTimes
?
DateTime a = DateTime.Now;
DateTime b = DateTime.Now.AddHours(1);
int differenceInMilliseconds;
The minus is overloaded, use the TimeSpan:
double differenceInMilliseconds = (b - a).TotalMilliseconds;
DateTime
values can be subtracted from one another resulting in an instance of a TimeSpan
.
So
DateTime a = DateTime.Now;
DateTime b = a.AddHours(1);
TimeSpan difference = b - a;
double differenceInMilliseconds = difference.TotalMilliseconds;
Note that TotalMilliseconds is a double
, not an int