Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get time difference in iPhone

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 ?

like image 876
New to iPhone Avatar asked Sep 15 '09 13:09

New to iPhone


People also ask

How do I show different times on my iPhone?

Use the Clock App To do so, open the Clock app on your iPhone or iPad and go to the “World Clock” tab. Here, tap the plus sign (+) in the top toolbar. Search for the city, and then tap it in the list to add it to the World Clock section. You now see the current time for that city in the World Clock section.

Do iPhones change to daylight savings automatically?

If you want to double check that your iPhone does, indeed, automatically change for daylight savings, you can do so easily in a matter of minutes. Head to your phone's Settings, then General, then Date & Time. The Set Automatically toggle should be switched on (so, you should see a green color).

What time will my iPhone change for Daylight Savings?

On Sunday, March 13, at 2 a.m. local time, the clock on every smartphone in the United States will adjust — or not adjust — to daylight saving time according to the time observed in your region, without your having to tap a finger.

Can you put 2 time zones on iPhone?

On an iPhone, you can add multiple cities/countries in the clock section but opening the clock app and then the World Clock section every time can be a pain. The free Klok app makes things simpler by adding multiple time zones into a widget.


1 Answers

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];
like image 193
Nathan de Vries Avatar answered Sep 22 '22 08:09

Nathan de Vries