1

I'm relatively new to programming for iOS using Xcode and Objective-C.

I need to be able to convert the length of a song, for example 3:31 that is a string into an integer representing seconds. so 3:31 (mm:ss) would be 211 seconds, and then back from 211 to 3:31.

Any help to get me started would be appreciated.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Paul S.
  • 1,342
  • 4
  • 22
  • 43

4 Answers4

7

You can split the string at :, and calculate the result like this:

NSArray* tokens = [lengthStr componentsSeparatedByString:@":"];
NSUInteger lengthInSeconds = 0;
for (int i = 0 ; i != tokens.count ; i++) {
    lengthInSeconds = 60*lengthInSeconds + [[tokens objectAtIndex:i] integerValue];
}

To format the value back, use

[NSString stringWithFormat:@"%d:%02d", lengthInSeconds / 60, lengthInSeconds % 60];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 2
    I think your code is backwards. You'll end up reading the seconds first, then on the second iteration, multiplying that by 60 and adding the minutes value. – Alex Feb 14 '12 at 17:39
  • @Alex You're right, and it also has an undeclared variable! Thanks for the correction. – Sergey Kalinichenko Feb 14 '12 at 17:50
7

To convert the time in seconds to the string you described, you can use the following code:

int songLength = 211;
int minutes = songLength / 60;
int seconds = songLength % 60;
NSString *lengthString = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];

Note the use of 0 in %02d This makes values like 188 transformed into 3:08 instead of 3:8.

You can use NSDateFormatter to get seconds and minutes from the time string:

NSString *timeString = @"3:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"mm:ss";
NSDate *timeDate = [formatter dateFromString:timeString];

formatter.dateFormat = @"mm";
int minutes = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = @"ss";
int seconds = [[formatter stringFromDate:timeDate] intValue];

int timeInSeconds = seconds + minutes * 60;

Edit: Adding hours

int songLength = 4657;
int hours = songLength / 3600;
int minutes = (songLength % 3600) / 60;
int seconds = songLength % 60;
NSString *lengthString = [NSString stringWithFormat:@"%d:%02d:%02d", hours, minutes, seconds];

And

NSString *timeString = @"2:3:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"hh:mm:ss";
NSDate *timeDate = [formatter dateFromString:timeString];

formatter.dateFormat = @"hh";
int hours = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = @"hh";
int minutes = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = @"ss";
int seconds = [[formatter stringFromDate:timeDate] intValue];

int timeInSeconds = seconds + minutes * 60 + hours * 3600;
sch
  • 27,436
  • 3
  • 68
  • 83
  • Wow, I wan't expecting this kind of turn around, thanks a bunch. I like this solution, but have a curve ball. What if there is more than 3600 seconds (1 hour). While the code will work great showing 60:00, what if there is more than an hours? For example: 4657 should return 1:17:37. I know there is probably a bumch of ways to do this, but what's the best? – Paul S. Feb 14 '12 at 19:25
  • I edited my answer to add hours. – sch Feb 14 '12 at 19:37
  • Would the best way to convert from seconds to hours to subtract 3600 from the songlength (or at least check to see if it's > 3600) and then you have your hours, and then the minutes and seconds as posted above? Thanks - Paul – Paul S. Feb 14 '12 at 19:37
  • I guess I was typing as you were updating. Thanks!! – Paul S. Feb 14 '12 at 19:38
1

NSString has tons of great methods to help with this type of thing.

You can use componentsSeperatedByString to break up your minutes and seconds

NSArray *listItems = [list componentsSeparatedByString:@":"];

then convert the strings to ints with intValue.

Finally, convert your minutes to seconds and add it all up.

You can get details on all the great things NSString does with the class refernce (NSString Reference)

AtkinsonCM
  • 376
  • 1
  • 14
0

This is a method I use in one of my apps to convert a long to mm:ss format:

long seconds = //anything;
NSString *output = [NSString stringWithFormat:@"%02lu:%02lu",seconds/60,seconds-(seconds/60)*60];

If you're using an int you just have to replace lu in the format with i like this:

[NSString stringWithFormat:@"%02i:%02i",seconds/60,seconds-(seconds/60)*60];

The 02 makes sure that the output is always two digits long. 65 seconds will be displayed as 01:05. If you would use %i:%i in the format the output for 65 seconds would look like this: 1:5 and thats not how you want it to look.

JonasG
  • 9,274
  • 12
  • 59
  • 88