3

我有一个 iOS 应用程序,它解析数据的 JSON 提要。在这个数据中是一些我存储在 NSString 中的 UNIX 时间戳。我想要做的是将这些时间戳转换为日期(月和日)。但是我试图在不做任何划分的情况下做到这一点,因为根据我在网上阅读的内容,您应该始终使用 Apple 的 API 进行转换以获得准确的结果。

但是我的代码不起作用,我收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 

这是我的代码:

NSString *time_string = [timestamps objectAtIndex:indexPath.row];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:time_string];

cell.dateOfPub.text = [NSString stringWithFormat:@"%@", date];

我不明白我做错了什么。任何帮助将不胜感激。

谢谢,丹

4

1 回答 1

7
double unixTimeStamp = [time_string doubleValue];
NSTimeInterval _interval=unixTimeStamp;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setLocale:[NSLocale currentLocale]];
[_formatter setDateFormat:@"dd.MM.yyyy"];
NSString *_date=[_formatter stringFromDate:date];
于 2013-10-16T15:54:55.877 回答