iOS Format String into minutes and seconds

前端 未结 7 1259
清酒与你
清酒与你 2020-12-10 12:56

I am receiving a string from the YouTube JSONC api, but the duration is coming as a full number i.e 2321 instead of 23:21 or 2 instead of 0:02. How would I go about fixing t

7条回答
  •  难免孤独
    2020-12-10 13:44

    Try this very optimized

    + (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];
    }
    

提交回复
热议问题