I have a value in milliseconds that I want to display in HH:MM format
Example:
I tried below logic but, didn't work.
func secondsToHourMinFormat(time: TimeInterval) -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute]
return formatter.string(from: time)
}
Your code is almost right, you just have a couple of omissions.
TimeInterval
is in seconds are you are passing milliseconds, so you need to divide by 1000.zeroFormattingBehaviour
to .pad
so that you don't get zero suppression in your outputstring(from:)
somehow; I have changed your function to return a String?
func secondsToHourMinFormat(time: TimeInterval) -> String? {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute]
formatter.zeroFormattingBehavior = .pad
return formatter.string(from: time/1000)
}