iPhone: How to get current milliseconds?

phil picture phil · Dec 11, 2008 · Viewed 243.7k times · Source

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

Answer

Allan Simonsen picture Allan Simonsen · Apr 30, 2010

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

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.

It returns the current absolute time, in seconds.


If you want only the decimal part (often used when syncing animations),

let ct = CACurrentMediaTime().truncatingRemainder(dividingBy: 1)