You can create a TimeInterval extension to format your elapsed time as follow:
Xcode 11.4 ? Swift 5.2 or later
extension TimeInterval {
var hourMinuteSecondMS: String {
String(format:"%d:%02d:%02d.%03d", hour, minute, second, millisecond)
}
var minuteSecondMS: String {
String(format:"%d:%02d.%03d", minute, second, millisecond)
}
var hour: Int {
Int((self/3600).truncatingRemainder(dividingBy: 3600))
}
var minute: Int {
Int((self/60).truncatingRemainder(dividingBy: 60))
}
var second: Int {
Int(truncatingRemainder(dividingBy: 60))
}
var millisecond: Int {
Int((self*1000).truncatingRemainder(dividingBy: 1000))
}
}
extension Int {
var msToSeconds: Double { Double(self) / 1000 }
}
let seconds = 131.531 // 131.531
let time = seconds.minuteSecondMS // "2:11.531"
let millisecond = seconds.millisecond // 531
let ms = 1111
let sec = ms.msToSeconds.minuteSecondMS // "0:01.111"
Date().description(with: .current) // "Wednesday, October 21, 2020 at 5:44:51 PM Brasilia Standard Time"
let startOfDay = Calendar.current.startOfDay(for: Date())
let secondsFromStartOfDay = Date().timeIntervalSinceReferenceDate - startOfDay.timeIntervalSinceReferenceDate
secondsFromStartOfDay.hourMinuteSecondMS // "17:44:51.705"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…