Hi,
I'm trying to display a string containing the number of the largest given time unit for the time since as given date - i.e. "2 Months Ago" or "1 Week Ago".
DateComponentsFormatter appears to be a useful tool for this (the app targets iOS 10.2), however it doesn't seem to be formatting the number of weeks consistently.
This is how I'm setting up the formatter:
import Foundation
let day = 60*60*24
let formatter = DateComponentsFormatter()
formatter.calendar = Calendar.current
formatter.allowedUnits = [.hour, .day, .weekOfMonth, .month]
formatter.unitsStyle = .full
formatter.maximumUnitCount = 1
formatter.allowsFractionalUnits = false
formatter.zeroFormattingBehavior = .dropAll
For the given range, I would expect 8-13 to use "1 week", but this doesn't seem to be the case:
(0..40).forEach {
print("\($0): \(formatter.string(from: TimeInterval(day * $0))!)")
}
The actual output of this is:
0: 0 hours
1: 1 day
2: 2 days
3: 3 days
4: 4 days
5: 5 days
6: 6 days
7: 1 week
8: 2 weeks
9: 2 weeks
10: 2 weeks
11: 2 weeks
12: 2 weeks
13: 1 week
14: 2 weeks
15: 2 weeks
16: 2 weeks
17: 2 weeks
18: 2 weeks
19: 2 weeks
20: 2 weeks
21: 3 weeks
22: 3 weeks
23: 3 weeks
24: 3 weeks
25: 3 weeks
26: 3 weeks
27: 3 weeks
28: 4 weeks
29: 4 weeks
30: 4 weeks
31: 1 month
32: 1 month
33: 1 month
34: 1 month
35: 1 month
36: 1 month
37: 1 month
38: 1 month
39: 1 month
Could anyone point me in the right direction?
5
0
2.2k