how can i define NSTimeInterval to mm:ss format in iphone? how can i define NSTimeInterval to mm:ss format in iphone? objective-c objective-c

how can i define NSTimeInterval to mm:ss format in iphone?


NSTimeInterval interval = 326.4;long min = (long)interval / 60;    // divide two longs, truncateslong sec = (long)interval % 60;    // remainder of long divideNSString* str = [[NSString alloc] initWithFormat:@"%02d:%02d", min, sec];

The %02d format specifier gives you a 2 digit number with a leading zero.

Note: this is for positive values of interval only.


See this question.

Accepted answer by Brian Ramsay is:

Given 326.4 seconds, pseudo-code:

minutes = floor(326.4/60)seconds = round(326.4 - minutes * 60)

If you print with %02d, you will get e.g. 03:08 if either number is less than 10.