NSTimeInterval to NSString formatted as NSDate

Hi,
I have two NSDates and want to display the time interval between these both dates as a string formatted in hours and minutes (H:mm). I was searching for it a long time, but didn't find a way to go through for this specific case.

iMac 2.0GHz Core Duo, Mac OS X (10.6.2), iPhone 3GS 3.1.3

Posted on Feb 26, 2010 4:29 AM

Reply
4 replies

Feb 26, 2010 6:44 AM in response to Tobe

Tobe wrote:
I already have the time interval as NSTimeInternal. The real problem is how format the time interval like a date into a string.


You can't. A timer interval is just a double. If you want to represent it as a formatted time string, you'll have to do it yourself. The following is standard procedure:

- (NSString *) formatInterval: (NSTimeInterval) interval
{
unsigned long seconds = interval;
unsigned long minutes = seconds / 60;
seconds %= 60;
unsigned long hours = minutes / 60;
minutes %= 60;
NSMutableString * result = [[NSMutableString new] autorelease];
if(hours)
[result appendFormat: @"%d:", hours];
[result appendFormat: @"%02:", minutes];
[result appendFormat: @"%02", seconds];
return result;
}


Enhance and/or fix bugs as needed.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

NSTimeInterval to NSString formatted as NSDate

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.