2012-05-04 230 views
0

我有以下代码:UNIX时间戳小时前

NSDate *commentPostedDate = [[NSDate alloc] initWithTimeIntervalSince1970:[self.newsFeedStream_.timestamp_ intValue]]; 
    NSLog(@"TIMESTAMP IS %@", self.newsFeedStream_.timestamp_); 

    int interval = (int) [[NSDate date] timeIntervalSinceDate:commentPostedDate]/60; 
    [commentPostedDate release]; 

    NSString *timePosted = [NSString stringWithFormat:@"%d hours ago", interval]; 

然而,这似乎并没有正确小时前返回。我在这里做错了什么?

回答

1

UNIX时间戳通常是以秒为单位的,那么如果你想要几个小时,你为什么要除以60呢?

有3600秒在一个小时,而不是60。

0

NSTimeInterval是含有秒数一个double值。特别是,你应该除以秒数在一小时内即为60*60

int interval = (int) ([[NSDate date] timeIntervalSinceDate:commentPostedDate]/3600);