2013-02-27 11 views
12

मुझे YouTube JSONC एपीआई से एक स्ट्रिंग प्राप्त हो रही है, लेकिन अवधि पूर्ण संख्या के रूप में 2321 या 2:02 के बजाय 2321 के बजाय आ रही है। मैं इसे ठीक करने के बारे में कैसे जाउंगा?आईओएस प्रारूप स्ट्रिंग मिनट और सेकंड में

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

+0

यह पता चला है कि मैं सही हूं। अवधि मान सेकंड की संख्या है। तो 2321 2321 सेकंड या 38 मिनट और 41 सेकंड या 38:41 है। नीचे मेरा जवाब देखें। – rmaddy

उत्तर

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

+0

अवधि मूल्य सीधे यूट्यूब फ़ीड – SquiresSquire

+0

से आ रहा है आपके पास कुछ गड़बड़ है। सत्यापित करें 'अवधि' वास्तव में सही मूल्य है। यदि आवश्यक हो, तो अवधि मान प्राप्त करने और घंटे और मिनट की गणना करने के लिए अपना वास्तविक कोड पोस्ट करें। – rmaddy

0

आप 2321subString और 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? @ "एस": @ ""] –

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

के लिए पाता है और उत्पादन belike इस

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)! 
संबंधित मुद्दे