转换时间戳 - IPhone

时间:2012-06-18 05:21:47

标签: iphone timestamp

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

1 个答案:

答案 0 :(得分: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];