如何在iPhone中获得时差 [英] How to Get time difference in iPhone

查看:116
本文介绍了如何在iPhone中获得时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个带有时间值的数组。它们采用以下格式。 mm:ss:几百秒。我想得到数组中两个[lastObjects]之间的区别。 NSDate无法正常工作,因为最后一个值是百分之一秒。

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.

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

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

推荐答案

你的问题有两个部分,解析时间并得到差异:

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

这篇关于如何在iPhone中获得时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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