简体   繁体   English

将NSString转换为Double for NSTimeInterval

[英]Convert NSString to Double for NSTimeInterval

The end result that I need is that I want to find out out how long ago a comment was posted. 我需要的最终结果是我想找出发表评论的时间。 To do this I started off by doing something like: 为此,我首先要做的是:

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

But that didn't work and returned something like 5213 months ago (it was only posted last week!) I then done some searching around and found out that the double value of the timestamp, ie 1377775454768 was actually 1377775454768.000000, so that explains why the timing was so wrong. 但这不起作用,并返回了类似于5213个月前的消息(仅在上周发布!),然后我进行了一些搜索,发现时间戳的两倍值,即1377775454768实际上是1377775454768.000000,因此可以解释为什么时间太错了。

I've now tried done some console logging and still cant get the double value with no decimal values for the NSTimeInterval. 我现在已经尝试完成一些控制台日志记录,但仍然无法获得NSTimeInterval的没有十进制值的双精度值。 This is what I have tried now: 这是我现在尝试过的:

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

And this is the result of the logging: 这是日志记录的结​​果:

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

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

You want the difference in time between now and when the comment was posted. 您需要现在和发布评论之间的时间差异。 Untested: 未经测试:

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

However you don't want to use NSDate to format this difference, as NSDate is for manipulating dates, not time deltas. 但是,您不想使用NSDate来格式化这种差异,因为NSDate是用于操纵日期而不是时间增量的。 You'll need to use the kind of code used here on Stackoverflow to show when answers were posted, for example if < 1 hour then show "X minutes ago", else show the date the comment was posted, etc. 您需要使用Stackoverflow上此处使用的那种代码来显示发布答案的时间,例如,如果<1小时后显示“ X分钟前”,否则显示评论的发布日期,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM