Compare 2 dates; check if interval has passed

rcaine

Newcomer
Joined
Mar 9, 2005
Messages
6
Hi all,

I'm trying to compare 2 DateTimes in C# to see if a specific time difference (5 seconds) has elapsed. I've been told that the best/most efficient way to compare them is to use a TimeSpan object in the code because it takes into account timing over midnight/new year/etc. However, I cannot see how to go about this.

Any help would be much appreciated.
 
If you use the .Subtract method of a DateTime, it returns a timespan which represents the 'TimeSpan' between the two dates... you can check the .TotalSeconds of the timespan to see if it is more than 5 seconds. :)
 
Thats great thanks :)

I found a way similar to yours by creating the above TimeSpan and also creating another TimeSpan of 5 seconds and then comparing them. I prefer your method though.
 
Back
Top