开发者

convert epoch time to NSDate in cocoa/iPhone

I 开发者_StackOverflow社区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


Documentation is your friend!

NSDate* date = [NSDate dateWithTimeIntervalSince1970:123456789];


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜