views:

383

answers:

2

how can i define NSTimeInterval to mm:ss format?

+1  A: 

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)

I would add (if you like):

if (seconds < 10)
    //Put a zero in front of seconds
if (minutes < 10)
    //Put a zero in front of minutes
Chris Cooper
+1  A: 
NSTimeInterval interval = 326.4;
long min = (long)interval / 60;    // divide two longs, truncates
long sec = (long)interval % 60;    // remainder of long divide
NSString* 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.

progrmr
Thank you! This is perfect
daidai