Swift - Integer conversion to Hours/Minutes/Seconds

后端 未结 23 1212
無奈伤痛
無奈伤痛 2020-11-28 01:47

I have a (somewhat?) basic question regarding time conversions in Swift.

I have an integer that I would like converted into Hours / Minutes / Second

23条回答
  •  抹茶落季
    2020-11-28 02:03

    In macOS 10.10+ / iOS 8.0+ (NS)DateComponentsFormatter has been introduced to create a readable string.

    It considers the user's locale und language.

    let interval = 27005
    
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.hour, .minute, .second]
    formatter.unitsStyle = .full
    
    let formattedString = formatter.string(from: TimeInterval(interval))!
    print(formattedString)
    

    The available unit styles are positional, abbreviated, short, full, spellOut and brief.

    For more information please read the documenation.

提交回复
热议问题