Skip to Content
Course content

70: DateFormatter and ISO8601DateFormatter

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this exact bug happen in more production apps than I care to admit. You're building a feature that fetches data from a JSON API, the dates look perfect in the API documentation, and the code works flawlessly on your simulator. Then, a user in a different country downloads the app, and suddenly, every single date in your UI is blank or showing as "January 1, 1970."

// The "It worked on my machine" approach
let dateString = "2023-10-25T14:30:00Z"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

if let date = formatter.date(from: dateString) {
    print("Success: \(date)")
} else {
    print("Failed to parse date") // This happens for some users!
}

The Locale Trap

The problem here is that DateFormatter is designed to be user-facing. By default, it respects the user's current locale and calendar settings. If a user has their device set to a non-Gregorian calendar (like the Buddhist or Japanese calendar), your hardcoded dateFormat string will fail because the year "2023" doesn't exist in the same way in those systems.

When you're dealing with data coming from a server, you aren't talking to a user; you're talking to a machine. Machines should use a standard that doesn't change based on where the device is located. This is where ISO8601DateFormatter comes in.

Standardizing with ISO8601DateFormatter

If your API provides dates in the ISO 8601 format (the one with the 'T' and the 'Z'), stop using DateFormatter entirely for the parsing step. ISO8601DateFormatter is specifically built for this. It ignores the user's local settings and follows the international standard, ensuring your app parses the date the same way in New York as it does in Tokyo.

let dateString = "2023-10-25T14:30:00Z"
let isoFormatter = ISO8601DateFormatter()

if let date = isoFormatter.date(from: dateString) {
    print("Parsed reliably: \(date)")
}

It's cleaner, it's faster to write, and most importantly, it's predictable. I always recommend using this for any Date object you're creating from a network response.

Converting Dates Back for the User

Now, you obviously shouldn't show "2023-10-25T14:30:00Z" to your users—that's ugly. To turn that Date object into something readable, you go back to DateFormatter. But here is the professional tip: avoid hardcoding strings like "MM/dd/yyyy" because people in Europe and the US read dates differently.

Instead, use setLocalizedDateFormatFromTemplate. This tells iOS: "I want the month, the day, and the year, but you decide the order based on the user's region."

let date = Date() // Assume this came from our ISO formatter
let displayFormatter = DateFormatter()
displayFormatter.setLocalizedDateFormatFromTemplate("yMMMd") 

// On a US device: "Oct 25, 2023"
// On a UK device: "25 Oct 2023"
let dateString = displayFormatter.string(from: date)

By splitting your logic—using ISO8601DateFormatter for the API and a templated DateFormatter for the UI—you eliminate a massive category of regional bugs. I've spent way too many hours debugging "nil dates" just to realize the user was in Thailand; save yourself that headache.




📋 Practical Task

Build a Flight Arrival Timestamp Converter

You are building a travel app. The API provides flight arrival times in ISO 8601 format, but you need to display them to the user in a friendly, localized way.

Your Task:

  • Create a constant string called apiTimestamp with the value "2024-12-12T18:45:00Z".
  • Use ISO8601DateFormatter to convert this string into a Swift Date object.
  • Use a DateFormatter with setLocalizedDateFormatFromTemplate to convert that Date object into a user-facing string that includes the full month, day, and year.
  • Print the final localized string to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.