如何在 iPhone 中获取时差

发布于 2024-08-04 23:47:38 字数 155 浏览 4 评论 0原文

我有 2 个包含时间值的数组。它们采用以下格式。 mm:ss:数百秒。我想获得数组中两个 [lastObjects] 之间的差异。 NSDate 不起作用,因为最后一个值以百分之一秒为单位。

一个问题。如果第二个日期大于第一个日期,它会给我一个负数,例如 -01:10:00 吗?

I have 2 arrays with time values in it. They are in the following format. mm:ss:hundreds of a sec. I want to get the difference between the two [lastObjects] in the arrays. NSDate is not working because the last value is in hundredsth of a sec.

A question. If the second date is larger than the first will it give me a negative number like -01:10:00 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-08-11 23:47:38

您的问题有两个部分,解析时间并获取差异:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"mm:ss:SS"];

NSDate* firstDate = [dateFormatter dateFromString:@"01:00:00"];
NSDate* secondDate = [dateFormatter dateFromString:@"01:02:00"];

NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

Your problem has two parts, parsing the time and getting the difference:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"mm:ss:SS"];

NSDate* firstDate = [dateFormatter dateFromString:@"01:00:00"];
NSDate* secondDate = [dateFormatter dateFromString:@"01:02:00"];

NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
忘年祭陌 2024-08-11 23:47:38

NSDate 应该适合你,使用 timeIntervalSinceDate:,返回毫秒精度的时间间隔。

NSDate should work for you, use timeIntervalSinceDate:, which returns a time interval which has millisecond precision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文