Swift - Integer conversion to Hours/Minutes/Seconds

后端 未结 23 1214
無奈伤痛
無奈伤痛 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:06

    NSTimeInterval is Double do do it with extension. Example:

    extension Double {
    
        var formattedTime: String {
    
            var formattedTime = "0:00"
    
            if self > 0 {
    
                let hours = Int(self / 3600)
                let minutes = Int(truncatingRemainder(dividingBy: 3600) / 60)
    
                formattedTime = String(hours) + ":" + (minutes < 10 ? "0" + String(minutes) : String(minutes))
            }
    
            return formattedTime
        }
    }
    

提交回复
热议问题