Monday, June 4, 2012

iOS: NSDate to Unix Timestamp (long) Format and Back

Getting current time

NSDate *now = [NSDate date];

Getting long from NSDate

long long nowAsLong = [now timeIntervalSince1970];

Getting NSDate from long


long long timestamp = xxxxxxx;
// in the API, the time interval is in seconds, not milliseconds
NSTimeInterval timeInterval = (double)(timestamp/1000);

NSDate *theDate = [[NSDate alloc]initWithTimeIntervalSince1970: timeInterval];

No comments:

Post a Comment