So my intention is to put out dates that would look like the following:
Today, August 28
Tomorrow, August 29
Friday, August 30
...etc
The issue
I use this approach in Swift 3:
struct SharedFormatters {
private static let dateWithRelativeFormatting: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .medium
df.doesRelativeDateFormatting = true
return df
}()
private static let dateWithoutRelativeFormatting: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .medium
df.doesRelativeDateFormatting = false
return df
}()
private static let longDateWithoutYear: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "MMMM d"
df.doesRelativeDateFormatting = false
return df
}()
static func string(from date: Date) -> String {
let val = dateWithRelativeFormatting.string(from: date)
let val2 = dateWithoutRelativeFormatting.string(from: date)
return val == val2 ? longDateWithoutYear.string(from: date) : val
}
}