1

我需要的最终结果是我想知道多久之前发布了一条评论。为此,我开始执行以下操作:

double timestampComment = [[testArray objectForKey:@"timestamp"] doubleValue]/1000;
NSDate *date = [NSDate date];
NSLog(@"Date: %@", date);
NSTimeInterval timestamp = (NSTimeInterval)timestampComment;
NSDate *actualTime = [NSDate dateWithTimeInterval:timestamp sinceDate:date];

但这不起作用并返回了类似 5213 个月前的内容(它仅在上周发布!)然后我进行了一些搜索,发现时间戳的双精度值,即 1377775454768 实际上是 1377775454768.000000,所以这就解释了为什么时机太不对了。

我现在已经尝试完成一些控制台日志记录,但仍然无法获得 NSTimeInterval 没有十进制值的双精度值。这是我现在尝试过的:

double timer = [[testArray objectForKey:@"timestamp"]doubleValue];
        //NSString *timerStr = [NSString stringWithFormat:@"%.0f", timer];
        NSString *timerCount = [testArray objectForKey:@"timestamp"];

        NSTimeInterval timeIntervalComment = timer;
        //NSTimeInterval timeIntervalCount = timerCount;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeIntervalComment];
        NSLog(@"Timer: %@", timerCount);
       NSString *rightTimestr = [NSString stringWithFormat:@"%.0f", timeIntervalComment];
        int timerTest = [rightTimestr intValue];
        NSTimeInterval timeTest = (NSTimeInterval)[timerCount doubleValue];
        NSDate *testDate = [NSDate dateWithTimeIntervalSince1970:timeTest];
        NSLog(@"rightTimestr %@", rightTimestr);
        NSLog(@"timerTest %d", timerTest);
        NSLog(@"%f", [timerCount doubleValue]);
        NSLog(@"Test Date: %@", testDate);
        NSLog(@"Timer 2: %f", timer);
        NSLog(@"date: %@", date);

这是日志记录的结​​果:

Timer: 1377775454768
rightTimestr 1377775454768
timerTest 2147483647
1377775454768.000000
Test Date: 45629-12-19 04:06:08 +0000
Timer 2: 1377775454768.000000
date: 45629-12-19 04:06:08 +0000

任何帮助,将不胜感激。谢谢

4

1 回答 1

0

您想要从现在到发表评论的时间差。未经测试:

double timestampComment = [[testArray objectForKey:@"timestamp"] doubleValue]/1000;
NSTimeInterval diff = (NSTimeInterval)timestampComment - [[NSDate date] timeIntervalSince1970];

// diff now contains the number of seconds since the comment was posted

但是,您不想NSDate用来格式化这种差异,就像NSDate操作日期而不是时间增量一样。您需要使用 Stackoverflow 上使用的代码来显示发布答案的时间,例如,如果 < 1 小时则显示“X 分钟前”,否则显示评论发布的日期等。

于 2013-09-03T13:05:17.950 回答