计算可可的时间差 [英] Calculate time difference in Cocoa

查看:128
本文介绍了计算可可的时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



在Cocoa中有一个简单的函数,使用60秒和分钟刻度来计算这两个整数之间的差值。



 时间1 = 161500 
时间2 = 171500

timedifference = 003000


解决方案

  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


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

解决方案

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

这篇关于计算可可的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆