I have the value of the epoch time like say 123456789, now I want to convert this into NSDate
in cocoa framework. Can anyone show me?
thanks
Since this is a high hit, going to contribute. Note that just converting to NSDate will put it into the UTC timezone. Then you have to convert over to your timezone if you want to display it to the user, from 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);
Documentation is your friend!
NSDate* date = [NSDate dateWithTimeIntervalSince1970:123456789];