14

我从 YouTube JSONC api 收到一个字符串,但持续时间是一个完整的数字,即 2321 而不是 23:21 或 2 而不是 0:02。我将如何解决这个问题?

JSON C

编辑:

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

NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
4

7 回答 7

24

假设持续时间值实际上是以秒为单位的持续时间,那么您可以计算分钟和秒数,然后将它们格式化为字符串。

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

NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
于 2013-02-27T21:44:05.233 回答
17

如果持续时间旨在面向用户,则应使用DateComponentsFormatter :

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [ .minute, .second ]
formatter.zeroFormattingBehavior = [ .pad ]
let formattedDuration = formatter.string(from: duration)!
于 2017-03-21T04:34:52.383 回答
7

试试这个非常优化

+ (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];
}
于 2014-08-26T08:53:39.043 回答
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);
于 2013-04-14T02:41:54.623 回答
1

是我为此找到的很棒的代码

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);

输出将是这样的

Dilip timeStr : 20:21
于 2013-12-02T14:58:07.407 回答
0

您可以subString2321第一个字符串设为 23,将第二个字符串设为 21,然后将它们转换为int. 还要检查文本的长度:

if (text.length < 4)
   //add zeros on the left of String until be of length 4
于 2013-02-27T21:44:14.770 回答
0

目标 C:

NSDateComponentsFormatter * formatter = [[NSDateComponentsFormatter alloc]init];
[formatter setUnitsStyle:NSDateComponentsFormatterUnitsStyleShort];
[formatter setAllowedUnits:NSCalendarUnitSecond | NSCalendarUnitMinute];
[formatter setZeroFormattingBehavior:NSDateComponentsFormatterZeroFormattingBehaviorPad];
return [formatter stringFromTimeInterval:duration];
于 2019-08-26T12:37:53.533 回答