2013-02-27 87 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)! 
相關問題