Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSDate into Unix timestamp in Objective C/iPhone? [duplicate]

How can I convert an NSDate to a Unix timestamp in Objective-C?

like image 688
sandy Avatar asked Jun 11 '10 13:06

sandy


People also ask

What is the format of Unix timestamp?

Unix epoch timestamps10 digit epoch time format surrounded by brackets (or followed by a comma). The digits must be at the very start of the message. For example, [1234567890] or [1234567890, other] followed by the rest of the message. 13 digit epoch time.

How Unix timestamp is calculated?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.

How many digits is a Unix timestamp?

php - How to convert a 13 digit Unix Timestamp to Date and time? - Stack Overflow.


2 Answers

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.

like image 45
Ed Marty Avatar answered Sep 21 '22 08:09

Ed Marty


I believe - [NSDate timeIntervalSince1970] is what you want. Jan 1, 1970 is the date of the Unix epoch.

like image 116
Brian Avatar answered Sep 23 '22 08:09

Brian