I should preface my question by saying I love this formatter and the edge cases it handles for us, but I've found a case that seems odd to me.
(all times are in the same time zone. let's go with Pacific)
current time is 3:05pm, December 21
comparison time formatter output
dec 20 @ 3:15pm 23 hours ago (great!)
dec 20 @ 2:15pm yesterday (also great!)
dec 19 @ 3:15pm yesterday (wait, wut?!)
If today is Dec 21, shouldn't yesterday be limited to Dec 20 00:00:00 - 23:59:59?
I see three options:
I'm using RelativeDateTimeFormatter incorrectly
I'm misunderstanding the sematics of 'yesterday' ( is it correct to refer to 47 hours ago as yesterday?)
RelativeDateTimeFormatter is misbehaving
all thoughts on this are welcome.foun
here is some playground code demonstrating this behaviour
let relativeDateFormatter: RelativeDateTimeFormatter = {
let formatter = RelativeDateTimeFormatter()
formatter.dateTimeStyle = .named
return formatter
}()
func startDate(from dateText: String) -> Date {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
formatter.timeZone = .current
return formatter.date(from: dateText)!
}
let myNow = startDate(from: "2022-12-21T23:05:00Z")
let almostTwoDaysAgo = myNow.addingTimeInterval(-(48 * 3600 - 600))
let relText = relativeDateFormatter.localizedString(for: almostTwoDaysAgo, relativeTo: myNow)
3
0
1.2k