首页 文章

转换时间戳 - IPhone [关闭]

提问于
浏览
-1

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

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];
    

相关问题