I am working on iOS Swift project. I am getting date string from server, But I have to show it whether its today or yesterday else date only.
So, I am doing timeIntervalSinceNow
property and trying following.
static func getDay(_ nowDay: String) -> String {
let dateString = "2021-02-09"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let journalDate = dateFormatter.date(from: dateString)!
let diff: TimeInterval = (journalDate.timeIntervalSinceNow ?? 0.0) * -1.0
if diff < 1 {
return "Today"
} else if diff < 2 {
return "Yesterday"
} else {
return nowDay
}
return nowDay
}
But, It is returning only date and not converting to Today or Yesterday.
Any suggestions?