iOS Format String into minutes and seconds

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

    Objective C:

    NSDateComponentsFormatter * formatter = [[NSDateComponentsFormatter alloc]init];
    [formatter setUnitsStyle:NSDateComponentsFormatterUnitsStyleShort];
    [formatter setAllowedUnits:NSCalendarUnitSecond | NSCalendarUnitMinute];
    [formatter setZeroFormattingBehavior:NSDateComponentsFormatterZeroFormattingBehaviorPad];
    return [formatter stringFromTimeInterval:duration];
    

提交回复
热议问题