I need to get the time elapsed between two events, for example, the appearance of a UIView and the user's first reaction.
How can I achieve it in Objective-C?
I need to get the time elapsed between two events, for example, the appearance of a UIView and the user's first reaction.
How can I achieve it in Objective-C?
NSDate *start = [NSDate date];
// do stuff...
NSTimeInterval timeInterval = [start timeIntervalSinceNow];
timeInterval is the difference between start and now, in seconds, with sub-millisecond precision.
Use the timeIntervalSinceDate method
NSTimeInterval secondsElapsed = [secondDate timeIntervalSinceDate:firstDate];
NSTimeInterval is just a double, define in NSDate like this:
typedef double NSTimeInterval;
For percise time measurements (like GetTickCount), also take a look at mach_absolute_time and this Apple Q&A: http://developer.apple.com/qa/qa2004/qa1398.html.
use the timeIntervalSince1970 function of the NSDate class like below:
double start = [startDate timeIntervalSince1970];
double end = [endDate timeIntervalSince1970];
double difference = end - start;
basically, this is what i use to compare the difference in seconds between 2 different dates. also check this link here