160

Hi all I am currently working on a project where when a certain event happens details about the event including the time that the event occurred is added into a list array.

Once the method has finished I then pass on the list to another method that checks its values. Before I go through the loop I check the current time of my PC and I need to check whether the difference between the time saved in the list array and the current time is greater than 5 seconds.

How would I go about doing this.

6 Answers 6

379

Assuming dateTime1 and dateTime2 are DateTime values:

var diffInSeconds = (dateTime1 - dateTime2).TotalSeconds; 

In your case, you 'd use DateTime.Now as one of the values and the time in the list as the other. Be careful of the order, as the result can be negative if dateTime1 is earlier than dateTime2.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, by the way, var will end up being double: TotalSeconds
Thanks... I've just got bitten by using (Milliseconds) by mistake when I should have really used (TotalMilliseconds) ... ouch.
If I do this with two dates that are 1-2 seconds apart I get 63774476745,095 seconds. What could be wrong?
var diffInSeconds = (dateTime1 - dateTime2).Seconds; is the good way to do it now.
@PierreMarsaa that doesn't do what you think it does. Look at the docs for that property, it says the value is always in the range -59 to 59.
31

DateTime has a Subtract method and an overloaded - operator for just such an occasion:

DateTime now = DateTime.UtcNow; TimeSpan difference = now.Subtract(otherTime); // could also write `now - otherTime` if (difference.TotalSeconds > 5) { ... } 

Comments

24

This version always returns the number of seconds difference as a positive number (same result as @freedeveloper's solution):

var seconds = System.Math.Abs((date1 - date2).TotalSeconds); 

Comments

4

I use this to avoid negative interval.

var seconds = (date1< date2)? (date2- date1).TotalSeconds: (date1 - date2).TotalSeconds; 

1 Comment

Use Math.Abs instead, this code is less readable and performant than just using the answer above.
3

The built-in DateTime.Subtract method can be used as follows

double diffInSeconds = dateTime1.Subtract(dateTime2).TotalSecond; if (diffInSeconds > 5) { /* do stuff */ } ; 

Comments

1

for .NET Framework

 int ticks = Environment.TickCount; /* Do something */ ticks = Environment.TickCount - ticks; // difference in milliseconds string elapsed = String.Format("Elapsed time: {0} s", (ticks/1000).ToString()); 

for repeated measurement

 int start_ticks, ticks; string elapsed; start_ticks = Environment.TickCount; /* Do something */ ticks = Environment.TickCount - start_ticks; // difference in milliseconds start_ticks += ticks; elapsed = String.Format("Elapsed time: {0} s", (ticks/1000).ToString()); /* Do something */ ticks = Environment.TickCount - start_ticks; // difference in milliseconds start_ticks += ticks; elapsed = String.Format("Elapsed time: {0} s", (ticks/1000).ToString()); 

with .NET Core it's better to use 64-bit TickCount64

 long ticks = Environment.TickCount64; ticks = Environment.TickCount64 - ticks; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.