Convert UTC date to any formatter easily

Pradeep
Aug 7, 2023

--

We are going to use ISO8601DateFormatter for this conversion. More about for this formattter.

Let's take a simple example :

Suppose we have this date string : “1975–05–21T07:00:00Z”

Small cheat sheet for date formatters:

Now we want our output in this format like “EEEE, MMM d, yyyy”

    static func releaseDateStr(_ releaseDate: String?) -> String {
guard let releaseDate = releaseDate else {return ""}
let utcISODateFormatter = ISO8601DateFormatter()

// Printing a Date
let date = Date()
print(utcISODateFormatter.string(from: date))

// Parsing a string timestamp representing a date
let utcDate = utcISODateFormatter.date(from: releaseDate)

let formatter = DateFormatter()
formatter.dateFormat = "EEEE, MMM d, yyyy"

if let date = utcDate {
return formatter.string(from: date)
}
return ""
}

That’s all, you got what you want

Happy Coding !!

--

--

Pradeep
Pradeep

Written by Pradeep

It's all about travelling, not destination ...⛴️

No responses yet