Last active
August 31, 2019 12:53
-
-
Save sharat/3cc9d6ff6f3e7c1e7ac5923c1ce5c0d6 to your computer and use it in GitHub Desktop.
Format time Interval to hour, minute, second format using Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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