2013-02-27 107 views
12

我收到的是来自YouTube JSONC API的字符串,但持续时间为全数,即2321而不是23:21或2而不是0:02。我将如何去解决这个问题?iOS格式的字符串分为几秒和几秒

JSON C

编辑:

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

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

你确定持续时间号是不是秒数?那么2321真的是38分41秒? – rmaddy 2013-02-27 21:42:36

+0

原来我是对的。持续时间值是秒数。所以2321是2321秒或38分41秒或38:41。请参阅下面的答案。 – rmaddy 2013-02-27 21:47:59

回答

22

假设持续时间值是真正的持续时间秒钟,然后就可以计算出分和秒的数,然后格式化这些成字符串。

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

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

显然,小时出错了。没有看到你的实际代码,就没有办法知道什么是错的。但很有可能你的“持续时间”值是从错误的值开始的。 – rmaddy 2013-02-28 03:18:18

+0

持续时间值是直接来自YouTube的饲料 – SquiresSquire 2013-02-28 03:24:23

+0

你有什么问题。验证'持续时间'确实是正确的值。如果需要,发布您的实际代码以获取持续时间值并计算小时和分钟。 – rmaddy 2013-02-28 03:27:45

0

可以subString2321,并获得第一个字符串为23,第二个作为21并将其转换为int。此外,检查文本的长度:

if (text.length < 4) 
    //add zeros on the left of String until be of length 4 
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); 
+0

我发现这里的条件格式化语法特别有用。具体来说:[年> 0? @“s”:@“”] – 2014-04-10 10:27:10

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 
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]; 
} 
5

您应该使用DateComponentsFormatter如果持续时间意在面向用户的:

let formatter = DateComponentsFormatter() 
formatter.allowedUnits = [ .minute, .second ] 
formatter.zeroFormattingBehavior = [ .pad ] 
let formattedDuration = formatter.string(from: duration)! 
相关问题