iOS Format String into minutes and seconds

前端 未结 7 1258
清酒与你
清酒与你 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:38

    Assuming the duration value is really the duration in seconds, then you can calculate the number of minutes and seconds and then format those into a string.

    int duration = ... // some duration from the JSON
    int minutes = duration / 60;
    int seconds = duration % 60;
    
    NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
    

提交回复
热议问题