Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Development: What's a simple way to calculate the number of seconds that have passed between two events?

Tags:

ios

iphone

ipad

I need to calculate the number of seconds that have passed between two events on the iPhone. To do so, I need to store the time that the first event occurred and check it against the time the second event occurred to see if more than 30 seconds has passed.

I'm about to begin trying to accomplish this using the NSDate class, but I was wondering if there's a simpler way to do this without using objects, as I would prefer to store simple, intrinsic values instead of objects.

Thanks for your wisdom!

like image 754
BeachRunnerFred Avatar asked Jan 11 '11 06:01

BeachRunnerFred


1 Answers

If you really want to avoid storing objects, you can do something like:

double startTime = [[NSDate date] timeIntervalSince1970];

//Run your other code

double endTime = [[NSDate date] timeIntervalSince1970];

if (endTime - startTime > 30) {
  //30 seconds have passed
}
like image 123
Sam Dufel Avatar answered Oct 09 '22 05:10

Sam Dufel