I'm using the Nasa API. It returns dates in raw string format: "date": "2022-01-15" (YYYY-MM-DD)
How do I format it to something like Jan 15th, 2022 using DateFormatter
and display it in a SwiftUI Text View?
private var dateFormatter = DateFormatter().configure {
$0.dateFormat = "yyyy-MM-dd"
$0.dateStyle = .medium
$0.doesRelativeDateFormatting = true
$0.locale = .current
$0.timeStyle = .medium
}
private var formattedDate: String {
guard let date = dateFormatter.date(from: apod.date) else { return "No date" }
return dateFormatter.string(from: date)
}
// Call site
Text(formattedDate)
It gives me "No date" on all items