cocoa touch NSDateFormatter and epoch date

匆匆过客 提交于 2020-01-16 02:55:06

问题


I have a string that is (I think) an epoch date. Here is the example string:

1372620996

Heres part of the code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *offerDate = [dateFormatter dateFromString:@"1372620996"];

This returns a null date, and I'm pretty sure my problem is because I haven't set a dateStyle on my NSDateFormatter. However, I'm not sure what dateStyle to use for an epoch date.


回答1:


It's actually simpler than that to convert to an NSDate;

NSString *dateStr = @"1372620996";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLog(@"%@", date);

> 2013-06-30 19:36:36 +0000


来源:https://stackoverflow.com/questions/17394555/cocoa-touch-nsdateformatter-and-epoch-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!