Skip to content

Instantly share code, notes, and snippets.

@sharat
Last active August 31, 2019 12:53
Show Gist options
  • Save sharat/3cc9d6ff6f3e7c1e7ac5923c1ce5c0d6 to your computer and use it in GitHub Desktop.
Save sharat/3cc9d6ff6f3e7c1e7ac5923c1ce5c0d6 to your computer and use it in GitHub Desktop.
Format time Interval to hour, minute, second format using Swift
extension TimeInterval {
func stringFormatted() -> String {
let interval = Int(self)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / (60*60)) % 60
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
}
// Test
let startTime: TimeInterval = Date.timeIntervalSinceReferenceDate - (59*60 + 120)
let endTime: TimeInterval = Date.timeIntervalSinceReferenceDate
(endTime - startTime).stringFormatted()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment