If I have an NSTimeInterval that is set to say 200.0, is there a way to convert that into 00:03:20, I was thinking I could initialise an NSDate with it and then use NSDateFo
In Swift 2, iOS 8+. This makes sure we only show hour when necessary
func format(duration: NSTimeInterval) -> String {
let formatter = NSDateComponentsFormatter()
formatter.zeroFormattingBehavior = .Pad
if duration >= 3600 {
formatter.allowedUnits = [.Hour, .Minute, .Second]
} else {
formatter.allowedUnits = [.Minute, .Second]
}
return formatter.stringFromTimeInterval(duration) ?? ""
}
So you have
print(format(12)) // 0:12
print(format(65)) // 1:05
print(format(1750)) // 29:10
print(format(3890)) // 1:04:50
print(format(45720)) // 12:42:00