14

I am receiving a string from the YouTube JSONC api, but the duration is coming as a full number i.e 2321 instead of 23:21 or 2 instead of 0:02. How would I go about fixing this?

JSON C

EDIT:

int duration = [videos valueForKey:@"duration"];
int minutes = duration / 60;
int seconds = duration % 60;

NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
SquiresSquire
  • 2,404
  • 4
  • 23
  • 39
  • Are you sure that duration number isn't the number of seconds? So 2321 is really 38 minutes and 41 seconds? – rmaddy Feb 27 '13 at 21:42
  • It turns out I am correct. The duration value is the number of seconds. So 2321 is 2321 seconds or 38 minutes and 41 seconds or 38:41. See my answer below. – rmaddy Feb 27 '13 at 21:47

7 Answers7

24

Assuming the duration value is really the duration in seconds, then you can calculate the number of minutes and seconds and then format those into a string.

int duration = ... // some duration from the JSON
int minutes = duration / 60;
int seconds = duration % 60;

NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Obviously the hour is coming out wrong. Without seeing your actual code there is no way to know what is wrong. But most likely your `duration` value is starting out with the wrong value. – rmaddy Feb 28 '13 at 03:18
  • the duration value is coming straight from the youtube feed – SquiresSquire Feb 28 '13 at 03:24
  • You have something wrong. Verify `duration` is really the right value. If needed, post your actual code for getting the duration value and calculating hour and minute. – rmaddy Feb 28 '13 at 03:27
  • Change `[videos valueForKey:@"duration"];` to `[[videos objectForKey:@"duration"] intValue];` – rmaddy Mar 04 '13 at 06:24
  • This is the cleanest, simplest solution by far. – CIFilter Jan 21 '17 at 00:43
17

You should use DateComponentsFormatter if the duration is intended to be user-facing:

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [ .minute, .second ]
formatter.zeroFormattingBehavior = [ .pad ]
let formattedDuration = formatter.string(from: duration)!
Niels
  • 301
  • 2
  • 6
7

Try this very optimized

+ (NSString *)timeFormatConvertToSeconds:(NSString *)timeSecs
{
    int totalSeconds=[timeSecs intValue];

    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
}
pteofil
  • 4,133
  • 17
  • 27
codercat
  • 22,873
  • 9
  • 61
  • 85
2
int sec = diff;//INFO: time in seconds

int a_sec = 1;
int a_min = a_sec * 60;
int an_hour = a_min * 60;
int a_day = an_hour * 24;
int a_month = a_day * 30;
int a_year = a_day * 365;

NSString *text = @"";
if (sec >= a_year)
{
    int years = floor(sec / a_year);
    text = [NSString stringWithFormat:@"%d year%@ ", years, years > 0 ? @"s" : @""];
    sec = sec - (years * a_year);
}

if (sec >= a_month)
{
    int months = floor(sec / a_month);
text = [NSString stringWithFormat:@"%@%d month%@ ", text, months, months > 0 ? @"s" : @""];
    sec = sec - (months * a_month);

}

if (sec >= a_day)
{
    int days = floor(sec / a_day);
text = [NSString stringWithFormat:@"%@%d day%@ ", text, days, days > 0 ? @"s" : @""];

    sec = sec - (days * a_day);
}

if (sec >= an_hour)
{
    int hours = floor(sec / an_hour);
text = [NSString stringWithFormat:@"%@%d hour%@ ", text, hours, hours > 0 ? @"s" : @""];

    sec = sec - (hours * an_hour);
}

if (sec >= a_min)
{
    int minutes = floor(sec / a_min);
text = [NSString stringWithFormat:@"%@%d minute%@ ", text, minutes, minutes > 0 ? @"s" : @""];

    sec = sec - (minutes * a_min);
}

if (sec >= a_sec)
{
    int seconds = floor(sec / a_sec);
text = [NSString stringWithFormat:@"%@%d second%@", text, seconds, seconds > 0 ? @"s" : @""];
}
   NSLog(@"<%@>", text);
Adrien G
  • 228
  • 1
  • 4
  • 12
1

Here is the great code I finds for this

int duration = 1221;
int minutes = floor(duration/60)
int seconds = round(duration - (minutes * 60))
NSString * timeStr = [NSString stringWithFormat:@"%i:%i",minutes,seconds];
NSLog(@"Dilip timeStr : %@",timeStr);

And the output will belike this

Dilip timeStr : 20:21
Community
  • 1
  • 1
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
0

You can subString the 2321 and get the first string as 23 and the second as 21 and convert them to int. Also check for the length of the text:

if (text.length < 4)
   //add zeros on the left of String until be of length 4
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
0

Objective C:

NSDateComponentsFormatter * formatter = [[NSDateComponentsFormatter alloc]init];
[formatter setUnitsStyle:NSDateComponentsFormatterUnitsStyleShort];
[formatter setAllowedUnits:NSCalendarUnitSecond | NSCalendarUnitMinute];
[formatter setZeroFormattingBehavior:NSDateComponentsFormatterZeroFormattingBehaviorPad];
return [formatter stringFromTimeInterval:duration];
Ofir Malachi
  • 1,145
  • 14
  • 20