How can I convert an NSDate to a Unix timestamp in Objective-C?
Asked
Active
Viewed 4.1k times
3 Answers
60
I believe - [NSDate timeIntervalSince1970]
is what you want. Jan 1, 1970 is the date of the Unix epoch.

Brian
- 15,599
- 4
- 46
- 63
-
6(long)[[NSDate date] timeIntervalSince1970]] – John Ballinger Feb 04 '14 at 21:36
-
2OP is asking how to convert *an NSDate*, not necessarily the current date – Brian Mar 10 '14 at 14:09
50
NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];
NSTimeInterval
is typedefed as a double, defined in seconds. If you want it in integer form, just cast the result to a long and use that instead, but this gives more precision.

Ed Marty
- 39,590
- 19
- 103
- 156