Reputation: 29
Hello I am trying to make a count down. I want to convert an integer from second to date and show in a label with this below formats. Lets say that 2.500.000 it prints out to label 29 days 22 hour
29 days 22 hour remained
15 hour 30 minutes remained
15 minutes remained
Upvotes: 1
Views: 805
Reputation: 131418
It sounds like you want a DateComponentsFormatter
. That lets you generate strings that describe amounts of time rather than dates.
You could use the function func string(from ti: TimeInterval) -> String?
which takes a time interval and converts it to a string describing an amount of time.
The following code, loosely based on the example code in Apple's documentation on DateComponentsFormatter:
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = true
formatter.includesTimeRemainingPhrase = true
formatter.allowedUnits = [.day, .hour, .minute, .second]
print(formatter.string(from: 123456.7) ?? "nil")
Generates the string About 1 day, 10 hours, 17 minutes, 36 seconds remaining
(At least with a US English locale.)
Upvotes: 2