Formatting Dates
Display dates with the modern formatting API.
Formatting Dates is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Formatting Dates” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Formatting Dates”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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.