Formatting Dates
Display dates with the modern formatting API.
Formatting Dates is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Formatting Dates
Raw Date values are not human-readable. Swift's modern FormatStyle API turns them into localized strings cleanly.
The formatted() Method
Calling .formatted() on a date gives a sensible default string for the user's locale.
import Foundation
let now = Date()
let text = now.formatted()
print("Produced a string: \(!text.isEmpty)")Date.FormatStyle
Pass a Date.FormatStyle to control which parts appear, choosing date and time styles independently.
import Foundation
let now = Date()
let s = now.formatted(date: .abbreviated, time: .omitted)
print("Has date only: \(!s.isEmpty)")Date and Time Styles
The date: and time: parameters accept .full, .long, .abbreviated, .numeric, or .omitted.
import Foundation
let now = Date()
let full = now.formatted(date: .complete, time: .shortened)
print(!full.isEmpty)Customizing with Components
The .dateTime style lets you compose exactly the fields you want, like .year().month().day().
import Foundation
let now = Date()
let s = now.formatted(.dateTime.year().month().day())
print("Custom fields: \(!s.isEmpty)")Choosing Field Width
Each component accepts options, e.g. .month(.wide) for the full name or .month(.twoDigits) for a numeric form.
import Foundation
let now = Date()
let s = now.formatted(.dateTime.month(.wide).year())
print(!s.isEmpty)Time Only
Compose just time fields when the date is implied, e.g. .hour().minute().
import Foundation
let now = Date()
let t = now.formatted(.dateTime.hour().minute())
print("Time string: \(!t.isEmpty)")Relative Formatting
RelativeFormatStyle produces phrases like "in 2 hours" or "yesterday" relative to now.
import Foundation
let soon = Date().addingTimeInterval(3600)
let rel = soon.formatted(.relative(presentation: .named))
print("Relative phrase produced: \(!rel.isEmpty)")ISO 8601
For machine-readable output use .formatted(.iso8601), the standard interchange format for APIs.
import Foundation
let now = Date()
let iso = now.formatted(.iso8601)
print("Contains T separator: \(iso.contains("T"))")Parsing Strings Back
Format styles are reversible: you can parse a string into a Date using the matching strategy.
import Foundation
let input = "2024-05-30T12:00:00Z"
if let date = try? Date(input, strategy: .iso8601) {
print("Parsed: \(date.timeIntervalSince1970 > 0)")
}FormatStyle vs DateFormatter
The new FormatStyle API is type-safe and faster to write than the older DateFormatter; prefer it in new code.
import Foundation
let now = Date()
print(now.formatted(date: .long, time: .omitted).isEmpty == false)Quick Check
Which call shows the date but omits the time?
Recap
The FormatStyle API formats dates with .formatted(), configurable date:/time: styles, composable .dateTime fields, relative phrases, and ISO 8601. It is reversible for parsing and preferred over DateFormatter. Next: time zones and locales.
Frequently asked questions
Is the “Formatting Dates” lesson free?
Yes — the full text of “Formatting Dates” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Formatting Dates”?
Display dates with the modern formatting API. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Formatting Dates” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.