我收到了来自YouTube JSONC api的字符串,但持续时间是一个完整的数字,即2321而不是23:21或2而不是0:02。我该如何解决这个问题?
编辑:
int duration = [videos valueForKey:@"duration"];
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
答案 0 :(得分:23)
假设持续时间值实际上是以秒为单位的持续时间,那么您可以计算分钟数和秒数,然后将其格式化为字符串。
int duration = ... // some duration from the JSON
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
答案 1 :(得分:10)
如果持续时间旨在面向用户,则应使用DateComponentsFormatter:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [ .minute, .second ]
formatter.zeroFormattingBehavior = [ .pad ]
let formattedDuration = formatter.string(from: duration)!
答案 2 :(得分:6)
试试这个非常优化的
+ (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];
}
答案 3 :(得分: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);
答案 4 :(得分:1)
Here是我为此找到的优秀代码
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
答案 5 :(得分:0)
您可以subString
2321
并将第一个字符串设为23,将第二个字符串设为21,并将其转换为int
。还要检查文本的长度:
if (text.length < 4)
//add zeros on the left of String until be of length 4
答案 6 :(得分:0)
目标C:
NSDateComponentsFormatter * formatter = [[NSDateComponentsFormatter alloc]init];
[formatter setUnitsStyle:NSDateComponentsFormatterUnitsStyleShort];
[formatter setAllowedUnits:NSCalendarUnitSecond | NSCalendarUnitMinute];
[formatter setZeroFormattingBehavior:NSDateComponentsFormatterZeroFormattingBehaviorPad];
return [formatter stringFromTimeInterval:duration];