iOS Format String into minutes and seconds

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

    You should use DateComponentsFormatter if the duration is intended to be user-facing:

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

提交回复
热议问题