How to convert ISO 8601 to date

How do I make a text label in a table view cell show "dd-MM-yyyy " instead of ISO 8601 retrieved from a web service ?

 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! NewsTableViewCell

        cell.authorName.text = articles?[indexPath.row].author

        cell.headLine.text = articles?[indexPath.row].title

        cell.newsImage.downloadImage(from:(self.articles?[indexPath.item].urlImage ?? "nill"))

        cell.timePublication.text = articles?[indexPath.row].publishedAt

    

        

        

 

        return cell

    }

    
You post many questions here, that's good.

But could you please review answers that are proposed and close the thread if the answer solved the problem ? That will help keep the forum clean.

Didn't you get the answer here ?
https://developer.apple.com/forums/thread/660412?answerId=632912022#632912022

Read with a date formatter in the format for 2020-08-22T18:55:36Z

Then build the date from it

and format in the new format
Following works, on the input described in other thread. Note the need to get rid of "T":
Code Block
let inputDate = "2020-08-22T18:55:36Z".replacingOccurrences(of: "T", with: " ")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
let date = dateFormatter.date(from: inputDate) ?? Date()
dateFormatter.dateFormat = "yyyy-MM-dd"
print(dateFormatter.string(from: date))


Or you can include 'T' in format:
Code Block
let inputDate = "2020-08-22T18:55:36Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: inputDate) ?? Date()
dateFormatter.dateFormat = "yyyy-MM-dd"


More details here:
https://stackoverflow.com/questions/38503489/how-to-convert-date-format-from-dd-mm-yyyy-to-yyyy-mm-dd-in-swift
Accepted Answer
If you want to work with a certain date format specified in ISO8601, you should better consider using ISO8601DateFormatter.

And creating a DateFormatter is relatively a heavy work in iOS, so you should create an instance outside the method once, and reuse it repeatedly in the method.

The code would be something like this:
Code Block
///To convert API result date (ISO8601) to `Date`, this property should not be inside any methods
let inDateFormatter = ISO8601DateFormatter()
///To convert `Date` to readable date format, this property should not be inside any methods
let outDateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "dd-MM-yyyy"
df.locale = Locale(identifier: "en_US_POSIX")
return df
}()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! NewsTableViewCell
cell.authorName.text = articles?[indexPath.row].author
cell.headLine.text = articles?[indexPath.row].title
cell.newsImage.downloadImage(from:(self.articles?[indexPath.item].urlImage ?? "nill"))
if let dateString = articles?[indexPath.row].publishedAt,
let date = inDateFormatter.date(from: dateString)
{
let formattedString = outDateFormatter.string(from: date)
cell.timePublication.text = formattedString
} else {
cell.timePublication.text = "----------"
}
return cell
}



One more, you should better consider adopting Decodable. Some specific date formats like ISO8601 can be converted into Date while decoding JSON.
My apologies, i'll be sure to do that before I post @claude31
@OOPer Thank you so much. Is that a closure or competed property ? The reason for my question is because of the () on line 10

Is that a closure or competed property ?

It is a closure (line 5 { ... line 10 }), immediately invoked (line 10()). The stored property outDateFormatter is initialized to the returned value of the closure. It's not a computed property.
How to convert ISO 8601 to date
 
 
Q