2010-04-09 15 views

risposta

49

Documentation è tuo amico!

NSDate* date = [NSDate dateWithTimeIntervalSince1970:123456789]; 
+13

Stack Overflow è mio amico più veloce. – tbondwilkinson

+0

Se la data arriva in millisecondi, devi prima dividerlo per 1000! –

7

Dal momento che questo è un grande successo, andando a contribuire. Si noti che la semplice conversione in NSDate lo inserirà nel fuso orario UTC. Poi è necessario convertire sopra al vostro fuso orario se si desidera visualizzare all'utente, da Convert epoch time to NSDate with good timezone with Objective c:

// Convert NSString to NSTimeInterval 
NSTimeInterval seconds = [epochTime doubleValue]; 

// (Step 1) Create NSDate object 
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds]; 
NSLog (@"Epoch time %@ equates to UTC %@", epochTime, epochNSDate); 

// (Step 2) Use NSDateFormatter to display epochNSDate in local time zone 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"]; 
NSLog (@"Epoch time %@ equates to %@", epochTime, [dateFormatter stringFromDate:epochNSDate]); 

// (Just for interest) Display your current time zone 
NSString *currentTimeZone = [[dateFormatter timeZone] abbreviation]; 
NSLog (@"(Your local time zone is: %@)", currentTimeZone); 
Problemi correlati