解决方法
NSDateFormatter不能做这样的事情;你将需要建立自己的规则.我想像:
- (NSString *)formattedDate:(NSDate *)date { NSTimeInterval timeSinceDate = [[NSDate date] timeIntervalSinceDate:date]; // print up to 24 hours as a relative offset if(timeSinceDate < 24.0 * 60.0 * 60.0) { NSUInteger houRSSinceDate = (NSUInteger)(timeSinceDate / (60.0 * 60.0)); switch(houRSSinceDate) { default: return [NSString stringWithFormat:@"%d hours ago",houRSSinceDate]; case 1: return @"1 hour ago"; case 0: NSUInteger minutesSinceDate = (NSUInteger)(timeSinceDate / 60.0); /* etc,etc */ break; } } else { /* normal NSDateFormatter stuff here */ } }
所以这是打印’x分钟前’或’x小时前’从日期起24小时,通常是一天.