gpt4 book ai didi

IOS 计算与现在的时差

转载 作者:行者123 更新时间:2023-11-29 02:52:26 29 4
gpt4 key购买 nike

我从服务器获取一个 Unix 时间戳(创建时间),我使用以下方法获取 NSDate 对象:

NSTimeInterval interval = [str doubleValue];    
NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];

我需要找出上面创建的时间和当前时间的时差,并以hh:mm:ss格式显示。我编码了:-

    NSTimeInterval timeDiff = [agent.chatStartTimeStamp timeIntervalSinceNow];
// NSDate *now = [NSDate date];
// timeDiff = [now timeIntervalSinceDate:agent.chatStartTimeStamp]; // RETURNS NEGATIVE

// Divide the interval by 3600 and keep the quotient and remainder
div_t h = div(timeDiff, 3600);
int hours = h.quot;
// Divide the remainder by 60; the quotient is minutes, the remainder
// is seconds.
div_t m = div(h.rem, 60);
int minutes = m.quot;
int seconds = m.rem;

NSString *str = [NSString stringWithFormat:@"%d:%d%d", hours, minutes, seconds];
cell.timeLabel.text = str;

NSLog(@"*********** CV CTRL - AGENT CHATSTARTTIME - %@ TIME DIFFERNCE = %f STR = %@", agent.chatStartTimeStamp, timeDiff, str);

上述 2 个代码的日志 -

AGENT CHATSTART TIME - 1403342129.980000  SET TIME - 2014-06-21 09:15:29 +0000

*********** CV CTRL - AGENT CHATSTARTTIME - 2014-06-21 09:15:29 +0000 TIME DIFFERNCE = 130.419857 STR = 0:210

上面的代码给我的结果是 - 假设值为 0:2:10,然后这个值减少到 0:1:40、0:1:6、0:-1....

我要注意的是 - 时间差应该增加,因为创建的时间将只是在当前时间之前/更早的时间。所以我希望从现在开始扣除 startTime 的值,即现在 - startTime(时间)。我相信这会给我带来我所期待的结果。我尝试使用 [now timeIntervalSinceDate:agent.chatStartTimeStamp]; 但返回否定响应。

更新

这就是我将 unix 时间戳转换为本地时间的方法:-

+ (NSDate *)getNSDateFromUnixTimeStamp : (NSString *) unixTime {
NSString *str = [NSString stringWithFormat:@"%f",[unixTime doubleValue]/(double)1000];

NSTimeInterval interval = [str doubleValue];

NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];

str = nil;
unixTime = nil;

return timeStamp;

并记录相同的内容:-

2014-06-23 12:24:38.046 MintChat[1021:70b] AGENT CHATSTART TIME - 1403506610.771000  SET TIME - 2014-06-23 06:56:50 +0000

我的系统时间是 12:24:38 & unixtimestamp 也是几秒前的当前时间,所以我想 unix 时间设置不应该也有几乎相同的时间。

任何人都可以帮助我如何获得这个简单的时差。我哪里出错了?我对这个主题进行了很多搜索,但无法得到预期的结果。

非常感谢任何帮助。

谢谢

最佳答案

您的注释代码 [now timeIntervalSinceDate:agent.chatStartTimeStamp]; 是正确的。来自 NSDate 文档 -

Return Value

The interval between the receiver and the current date and time. If the receiver is earlier than the current date and time, the return value is negative.

所以,你可以简单地取绝对值来得到秒数——

NSTimeInterval timeDiff = fabs([now timeIntervalSinceDate:agent.chatStartTimeStamp]);

一旦你有了时间间隔,你就可以使用 this answer格式化它-

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeDiff];    
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"hh:mm:ss %@", formattedDate);

关于IOS 计算与现在的时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340327/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com