I'm storing time in seconds and would like to display them in minutes and seconds like "2m 6s". Ideally this would be done with localization. How can I accomplish this?
Asked
Active
Viewed 64 times
-3
-
@JoshCaswell That is not the format I'm after at all. Should I edit my question to be clearer? I only want minutes and seconds with an 'm' and an 's' following the numbers. – circuitlego Jul 28 '13 at 00:04
-
No, you should [search](http://stackoverflow.com/search?q=format+nsdate), read [the Data Formatting Guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html%23//apple_ref/doc/uid/TP40002369-SW4), and try something yourself. We have enough questions about `NSDateFormatter` here; everybody should be able to figure out their own pet format at this point. – jscs Jul 28 '13 at 00:11
1 Answers
2
This really boils down to standard math.
If you have the seconds, just divide it by 60 to get the number of minutes. For the remaining seconds, simply take the modulus of the total amount of seconds by 60.
e.g.
int minutes = seconds / 60;
int seconds = seconds % 60;
NSString *formattedTime = [NSString stringWithFormat:@"%im %is", minutes, seconds];
It's actually quite simple!

eswick
- 519
- 4
- 16
-
2You could also do this in one step with the library function [`div()`](http://developer.apple.com/library/ios/#documentation/system/conceptual/manpages_iphoneos/man3/div.3.html). – jscs Jul 28 '13 at 00:15