A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Objective-C.
To run the program, save it as Epoch.m and compile it with:
In Objective-C, we use NSDate to work with dates and times. The timeIntervalSince1970 method gives us the number of seconds since the Unix epoch. We can then convert this to milliseconds or nanoseconds by multiplying.
To create an NSDate from seconds or nanoseconds since the epoch, we use the dateWithTimeIntervalSince1970: class method.
Next, we’ll look at another time-related task: time parsing and formatting.