缩短日期后,NSdateFormatter返回null

时间:2013-11-06 23:05:14

标签: ios iphone objective-c nsdate nsdateformatter

我正在尝试以MM / dd格式显示推文的日期,但它返回null。 longDate很好,并以长格式显示正确的日期。

NSDate *createdDate = [NSDate dateWithTimeIntervalSince1970:[[[sortedEvents objectAtIndex:indexPath.row] objectForKey:@"created_time"] doubleValue]];
NSString *dateString = [NSString stringWithFormat:@"%@", createdDate];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *longDate = [dateFormat dateFromString: dateString];

[dateFormat setDateFormat:@"MM/dd"];
NSString *dateString2 = [NSString stringWithFormat:@"%@", longDate];
NSDate *condensedDate = [dateFormat dateFromString:dateString2];

1 个答案:

答案 0 :(得分:0)

您有几个不需要的步骤。由于你从一个日期开始,只需这样做:

NSDate *createdDate = [NSDate dateWithTimeIntervalSince1970:[[[sortedEvents objectAtIndex:indexPath.row] objectForKey:@"created_time"] doubleValue]];
[dateFormat setDateFormat:@"MM/dd"];
NSString *condensedString = [dateFormat stringFromDate:longDate];

你不需要来回做所有其他事情。