-1

我试图找出如何转换时间戳日期和时间(格式 - 2012-06-14T13:15:21Z)以将其显示在屏幕上,如“1 小时前”/“2 天前”/“2 秒前”等。谁能帮我这个?

4

1 回答 1

2

尝试这个:

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date1 = yourDate;
NSDate *date2 = [NSDate date];//Current Date
unsigned int unitFlags = NSDayCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;                   
NSDateComponents *diffComps = [cal components:unitFlags fromDate:date2 toDate:date1 options:0];
[diffComps month] //gives you month
[diffComps day] //gives you day
[diffComps year] // gives you year
[cal release];
于 2012-06-18T05:53:42.600 回答