0

我有以下代码:

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

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

4

2 回答 2

1

UNIX timestamps are usually in seconds, so why are you dividing by 60 if you want hours?

There are 3600 seconds in an hour, not 60.

于 2012-05-04T22:40:47.267 回答
0

NSTimeInterval是一个double包含秒数的值。特别是,您应该除以一小时内的秒数,即60*60

int interval = (int) ([[NSDate date] timeIntervalSinceDate:commentPostedDate] / 3600);
于 2012-05-04T22:41:17.833 回答