Reputation: 3745
I am getting some date string from server, like "2021-06-25"
If it is today date or yesterday date, I have to show like Today or Yesterday. Else I have to show 25 June 2021 For Today and Yesterday date its working fine.
But, If its other day, it is showing 25 Jun 2021
How to show 21 June 2021
static func getDay(_ nowDay: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let journalDate = dateFormatter.date(from: nowDay)
if let convertedDate = journalDate {
let relativeDateFormatter = DateFormatter()
relativeDateFormatter.timeStyle = .none
relativeDateFormatter.dateStyle = .medium
relativeDateFormatter.locale = Locale(identifier: "en_GB")
relativeDateFormatter.doesRelativeDateFormatting = true
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yyyy-MM-dd"
return relativeDateFormatter.string(from: convertedDate)
}
return ""
}
Any suggestions?
Upvotes: 0
Views: 127
Reputation: 270980
If you want the full name of the month, use .long
rather than .medium
as the date style:
relativeDateFormatter.dateStyle = .long
Also note that you can use a ISO8601DateFormatter
to parse nowDay
, since it is in ISO-8601 format:
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withFullDate]
let journalDate = dateFormatter.date(from: nowDay)
Upvotes: 4