Format ISO 8601 dates in Swift

How Swift turns Date values into ISO 8601 strings, what that formatting changes and loses, and when a localized display string serves readers better.

Formatting a date as ISO 8601 in Swift converts a point in time into a fixed, machine-friendly string such as a year-month-day date followed by a time and a time zone marker. This review covers whether that conversion suits your purpose, what it changes, and how the result behaves once it leaves your app. It relies on documented Foundation behavior and general principles of the standard. No hands-on test results were supplied, so any claim about a specific destination or assistive technology is marked as a gap.

Is ISO 8601 formatting the right choice for your use?

For data exchange, the answer is usually yes. ISO 8601 strings are unambiguous about field order, independent of the user's locale, and widely accepted by APIs, databases and log tooling. That is the main benefit. The decisive tradeoff is readability for people: the format ignores regional conventions, uses 24-hour time, and often shows UTC with a trailing Z, which many readers will not interpret as their local time. If a person is the audience rather than a program, a localized display string is generally the better choice, and the ISO form can stay behind the scenes.

  • Choose ISO 8601 for JSON payloads, server APIs, stored timestamps, logs and any text another program must parse.
  • Choose ISO 8601 when values must sort correctly as plain text, provided every value uses the same time zone and precision.
  • Prefer a localized string, for example from date.formatted(date: .abbreviated, time: .shortened), for labels, notifications and anything read aloud or shown to end users.
  • Avoid the extended ISO time form in file names meant for Windows, because the colon is not a permitted file name character there; use the basic form without separators or a different layout instead.

What the transformation actually changes

In this context, "format" means serialization, not typeface. Nothing is reversed, mirrored or swapped for styled Unicode look-alikes, and no CSS or display-layer trick is involved. A Swift Date stores only an instant in time. The formatter writes that instant out as ordinary characters: ASCII digits, hyphens, colons, the letter T between date and time, and either Z or a numeric offset such as -05:00. Those characters are what gets stored and transmitted; how they look depends only on the font of whatever displays them.

Two settings change the characters without changing the instant. The time zone decides whether the same moment is written as UTC with Z or as local time with an offset; ISO8601DateFormatter defaults to GMT. The precision decides whether fractional seconds appear; they are omitted unless you request them. Note what cannot be recovered: because a Date holds no time zone, the offset in the output reflects the formatter's setting, not where the event happened. If the original offset matters, store it separately.

import Foundation

let formatter = ISO8601DateFormatter()
// Defaults: .withInternetDateTime, GMT time zone
let stamp = formatter.string(from: Date())
// Shape: 2025-01-15T09:30:00Z (illustrative)

formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let precise = formatter.string(from: Date())
// Shape: 2025-01-15T09:30:00.123Z (illustrative)

formatter.timeZone = TimeZone(identifier: "America/New_York")
// Same instant, now written with a local offset such as -05:00

On iOS 15, macOS 12 and later releases, Date.ISO8601FormatStyle offers the same output through the newer formatting API, and it doubles as a parse strategy for reading strings back into dates.

let now = Date()
let text = now.formatted(.iso8601)
let withFraction = now.formatted(Date.ISO8601FormatStyle(includingFractionalSeconds: true))

let parsed = try Date(text, strategy: .iso8601)

Round trips fail silently or throw when options do not match. A formatter configured without fractional seconds will not parse a string that contains them, and the reverse is also true. If incoming data is inconsistent, try one configuration and fall back to the other rather than assuming a single setting covers every source.

If you must use DateFormatter with a custom pattern, fix the locale to en_US_POSIX so user settings such as 12-hour time or non-Gregorian calendars cannot alter the output, and use lowercase yyyy. Uppercase YYYY is the week-based year and produces wrong values around the turn of the year.

let legacy = DateFormatter()
legacy.locale = Locale(identifier: "en_US_POSIX")
legacy.timeZone = TimeZone(secondsFromGMT: 0)
legacy.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
Approach | Best for | Main caveat
ISO8601DateFormatter | Broad OS support, explicit option sets | Fractional seconds and time zone must be configured deliberately
Date.ISO8601FormatStyle | Newer OS targets, formatting and parsing in one type | Requires iOS 15, macOS 12 or later
DateFormatter with a pattern | Nonstandard variants a server demands | Easy to break without en_US_POSIX and the correct year symbol
JSONEncoder .iso8601 strategy | Quick Codable support | Does not include fractional seconds

Will the result stay readable and usable after copying or sharing?

Because the output is plain ASCII with no combining marks, emoji or invisible characters, copying and pasting it should not alter it, and editing or searching it behaves like any other text. Strings with the same time zone and the same precision also sort chronologically as text, which is useful in logs and file listings. Mixing offsets or mixing values with and without fractional seconds breaks that ordering.

Display direction is the main presentation risk. Hyphens and colons are neutral characters under the Unicode bidirectional algorithm, so an ISO timestamp embedded in Arabic or Hebrew text may be shown with its parts in an unexpected visual order even though the stored characters are unchanged. Wrapping the timestamp in directional isolates, such as first strong isolate (U+2068) and pop directional isolate (U+2069), or showing a localized date instead, reduces that risk. No rendering test for a specific app was supplied, so verify in the destinations you care about.

Accessibility is an open question in this review. There is no supplied documentation or scoped test showing how VoiceOver or other screen readers announce a raw ISO 8601 string, so no claim is made about it. The safer practice is to treat the ISO form as data and give people a localized, plain-language equivalent wherever a date is shown or spoken.

Gaps in this review

The guidance above rests on documented Foundation APIs and general properties of ISO 8601 and Unicode. It does not include measured results, screen reader recordings or tests in named apps or services. Before relying on ISO strings in a user-facing or right-to-left context, confirm the behavior in your own target environments.