Swift: What day is today?
Video: Swift: What day is today? by Taught by Celeste AI - AI Coding Coach
Swift with Copilot: What Day Is Today?
Date()is "now." Convert to a weekday name withDateFormatter(EEEEfor full,EEEfor abbreviated). Or useCalendar.current.component(.weekday, from:)for the integer.
A practical exercise. Date formatting in Swift is verbose; once you've seen it, the pattern is clear.
The Copilot prompt
// Print what day of the week today is
import Foundation
Copilot completes:
import Foundation
let formatter = DateFormatter()
formatter.dateFormat = "EEEE"
let today = formatter.string(from: Date())
print("Today is \(today)")
// Today is Wednesday
Walkthrough
Date() — current instant
let now = Date()
print(now)
// 2026-05-08 16:32:14 +0000
Date is a moment in time, internally a Double (seconds from Apple's reference date). It's not bound to a calendar or time zone — those come in when you format it.
DateFormatter
let formatter = DateFormatter()
formatter.dateFormat = "EEEE" // full weekday name
let weekday = formatter.string(from: Date())
// "Wednesday"
dateFormat is a Unicode date pattern. Common tokens:
| Token | Meaning | Example |
|---|---|---|
EEEE |
Weekday full | Wednesday |
EEE |
Weekday abbrev | Wed |
MMMM |
Month full | May |
MMM |
Month abbrev | May |
yyyy |
Year 4-digit | 2026 |
MM |
Month 2-digit | 05 |
dd |
Day 2-digit | 08 |
HH |
Hour 24h | 14 |
mm |
Minute | 32 |
formatter.dateFormat = "yyyy-MM-dd"
formatter.string(from: Date())
// "2026-05-08"
Locale-aware
formatter.locale = Locale(identifier: "fr_FR")
formatter.dateFormat = "EEEE"
formatter.string(from: Date())
// "mercredi"
For user-facing strings, set the locale (or use the user's default). For machine-readable formats (logs, IDs), use Locale(identifier: "en_US_POSIX") to get a stable English format regardless of user settings.
Better: dateStyle and timeStyle
For locale-correct formatting, prefer dateStyle:
let f = DateFormatter()
f.dateStyle = .full // "Wednesday, May 8, 2026"
f.dateStyle = .long // "May 8, 2026"
f.dateStyle = .medium // "May 8, 2026"
f.dateStyle = .short // "5/8/26"
f.string(from: Date())
For just weekday, you'd combine:
f.dateStyle = .full
f.timeStyle = .none
// "Wednesday, May 8, 2026"
Then split the string. Or stick with dateFormat = "EEEE" for raw weekday only.
Modern alternative: Date.FormatStyle (iOS 15+)
let weekday = Date().formatted(.dateTime.weekday(.wide))
// "Wednesday"
let date = Date().formatted(.dateTime.year().month().day())
// "May 8, 2026"
let custom = Date().formatted(
.dateTime.weekday(.wide).month(.wide).day().year()
)
Date.FormatStyle is the new, type-safe API. Each component is a method that returns a chainable style. Available on iOS 15+, macOS 12+.
For older deployment targets, stick with DateFormatter.
Calendar component access
If you want the integer weekday (1-7):
let weekday = Calendar.current.component(.weekday, from: Date())
print(weekday)
// 4 (Sunday=1, Saturday=7 in Gregorian)
Calendar.current is the user's calendar (usually Gregorian). component(_:from:) extracts:
.year,.month,.day,.hour,.minute,.second.weekday,.weekOfYear,.weekOfMonth.dayOfYear,.quarter
let cal = Calendar.current
let now = Date()
print(cal.component(.year, from: now)) // 2026
print(cal.component(.month, from: now)) // 5
print(cal.component(.day, from: now)) // 8
To map weekday integer to name:
let formatter = DateFormatter()
let weekdays = formatter.standaloneWeekdaySymbols
// ["Sunday", "Monday", "Tuesday", ...]
let n = Calendar.current.component(.weekday, from: Date())
print(weekdays[n - 1]) // -1 because weekday is 1-indexed
Time zones
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm zzz"
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.string(from: Date())
// "2026-05-08 16:32 UTC"
formatter.timeZone = TimeZone(identifier: "America/New_York")
formatter.string(from: Date())
// "2026-05-08 12:32 EDT"
Default is the user's current time zone. For server-side or logging, use UTC explicitly.
Common stumbles
import Foundation missing. Date, DateFormatter, Calendar are in Foundation, not the Swift core. Required at the top.
weekday is 1-indexed. Sunday is 1, Saturday is 7. The standalone weekday symbol array is 0-indexed.
Mixing user locale and machine formats. For storage/JSON/logs: en_US_POSIX locale and ISO 8601 format. For UI: user's default locale.
Date arithmetic with seconds. Date(timeIntervalSinceNow: 86400) works for "tomorrow" most of the time. For DST-correct math, use Calendar.date(byAdding:value:to:).
Format string typos. mm is minutes, MM is months. hh is 12-hour, HH is 24-hour. Easy to mix up — read the table carefully.
Optional parsing. formatter.date(from: "bad") returns nil, not an error. Always handle the optional.
What's next
Episode 6: Exact day of 2023-01-02. Parsing a date string back into weekday.
Recap
Date() is now. DateFormatter with dateFormat = "EEEE" for weekday name. Locale matters — set en_US_POSIX for stable machine-readable, user's locale for UI. Calendar.current.component(.weekday, from:) for integer weekday. Modern API: Date().formatted(.dateTime.weekday(.wide)) (iOS 15+).
Next episode: parsing a date.