计算可可的时差

发布于 2024-07-15 09:05:09 字数 213 浏览 5 评论 0原文

我有两个时间值,格式为:%H%M%S (EG161500)

这些值是基于文本的整数。

Cocoa 中是否有一个简单的函数可以使用 60 秒和分钟的刻度来计算这两个整数之间的差异?

因此,如果

time 1 = 161500
time 2 = 171500

timedifference = 003000

I have got two timevalues in the format: %H%M%S (E.G.161500)

These values are text-based integers.

Is there a simple function in Cocoa that calculates the difference between these two integers using a 60 seconds and minutes scale?

So if

time 1 = 161500
time 2 = 171500

timedifference = 003000

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

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

发布评论

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

评论(4

箹锭⒈辈孓 2024-07-22 09:05:09
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HHmmss"];

NSDate *date1 = [dateFormatter dateFromString:@"161500"];
NSDate *date2 = [dateFormatter dateFromString:@"171500"];

NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // diff = 3600.0
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HHmmss"];

NSDate *date1 = [dateFormatter dateFromString:@"161500"];
NSDate *date2 = [dateFormatter dateFromString:@"171500"];

NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // diff = 3600.0
风筝有风,海豚有海 2024-07-22 09:05:09

用于操作日期的类是NSDate。 获取时间间隔的方法是-timeIntervalSinceDate:。 结果是一个 NSTimeInterval 值,它是一个 double 表示间隔(以秒为单位)。

您可以使用 +dateWithString:NSString 创建 NSDate 对象,前提是您的日期格式为2001-03-24 10:45:32 +0600

The class for manipulating dates is NSDate. The method for getting time intervals is -timeIntervalSinceDate:. The result is a NSTimeInterval value, which is a double representing the interval in seconds.

You can create a NSDate object from a NSString with +dateWithString:, provided that your date is formatted as 2001-03-24 10:45:32 +0600.

乖不如嘢 2024-07-22 09:05:09

试试这个代码。

- (NSTimeInterval)intervalBetweenDate:(NSDate *)dt1 andDate:(NSDate *)dt2 {

NSTimeInterval interval = [dt2 timeIntervalSinceDate:dt1];
NSLog(@"%f",interval);
return interval;

}

try this code.

- (NSTimeInterval)intervalBetweenDate:(NSDate *)dt1 andDate:(NSDate *)dt2 {

NSTimeInterval interval = [dt2 timeIntervalSinceDate:dt1];
NSLog(@"%f",interval);
return interval;

}

知你几分 2024-07-22 09:05:09

我将创建一个 NSFormatter 子类来从输入数据中解析该格式的时间值(您可以将其放在文本字段上以自动转换用户输入,或使用它从代码中的数据源进行解析)。 让它以包含在 NSNumber 中的 NSTimeInterval(双精度代表秒)的形式返回总秒数。 从那里可以很容易地减去差异,并使用您创建的相同 NSFormatter 类显示它。 在解析和显示值时,您负责编写从秒转换为小时:分钟:秒或任何您喜欢的格式的代码。 如果对您的应用程序有意义,您还可以将这些值转换为像 mouviciel 提到的 NSDate。 请记住,您始终会存储与参考日期的时差,通常是 1970 年 1 月 1 日(NSDate 有方法为您执行此操作)。

I would create an NSFormatter subclass to parse time values in that format from input data (you can put one on a text field to automatically convert user input, or use it to parse from a data source in code). Have it return the combined number of seconds as an NSTimeInterval (double representing seconds) wrapped in an NSNumber. From there it's easy to subtract the difference, and display it using the same NSFormatter class you created. In both parsing and displaying values, you're the one responsible to write code converting from seconds to hours:minutes:seconds or whatever format you like. You could also convert these values to an NSDate like mouviciel mentioned, if it makes sense for your application. Just keep in mind you're always going to be storing the time difference from a reference date, usually Jan 1st 1970 (NSDate has methods to do this for you).

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