"created_at" = "Sat Apr 05 06:31:57 +0000 2014";
以下方法返回 twit 出现之前的多少时间。
-(NSString*)retrivePostTime:(NSString*)postDate{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"eee MMM dd HH:mm:ss ZZZZ yyyy"];
NSDate *userPostDate = [df dateFromString:postDate];
NSDate *currentDate = [NSDate date];
NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:userPostDate];
NSTimeInterval theTimeInterval = distanceBetweenDates;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSString *returnDate;
if ([conversionInfo month] > 0) {
returnDate = [NSString stringWithFormat:@"%ldmth ago",(long)[conversionInfo month]];
}else if ([conversionInfo day] > 0){
returnDate = [NSString stringWithFormat:@"%ldd ago",(long)[conversionInfo day]];
}else if ([conversionInfo hour]>0){
returnDate = [NSString stringWithFormat:@"%ldh ago",(long)[conversionInfo hour]];
}else if ([conversionInfo minute]>0){
returnDate = [NSString stringWithFormat:@"%ldm ago",(long)[conversionInfo minute]];
}else{
returnDate = [NSString stringWithFormat:@"%lds ago",(long)[conversionInfo second]];
}
return returnDate;
}