views:

8261

answers:

2

What is the best way to get the current system time milliseconds?

+16  A: 

[[NSDate date] timeIntervalSince1970];

It returns the number of seconds since epoch as a double. I'm almost sure you can access the milliseconds from the fractional part.

codelogic
+10  A: 

If you're looking at using this for relative timing (for example for games or animation) I'd rather use

    double CurrentTime = CACurrentMediaTime(); 

Which is the recommended way; NSDate draws from the networked synch-clock and will occasionally hiccup when re-synching it against the network.

Allan

Allan Simonsen
Good tip. -[NSDate timeIntervalSince1970] can return a value that is actually less than a value that it returned in the past, because it's based on the system clock. This clock is synchronized with the current time over the network (e.g. cell network or Internet). The Core Animation function CACurrentMediaTime "apparently takes care of the tricky bits in converting mach_absolute_time() into seconds." (http://www.alexcurylo.com/blog/2009/09/05/tip-cacurrentmediatime/)
Elliot