Time Zones and Locales
Handle zone and locale differences correctly.
Time Zones and Locales is a free Swift Academy lesson on CoddyKit — lesson 4 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.
Time Zones and Locales
The same Date instant displays differently around the world. TimeZone and Locale decide how it is interpreted and shown.
TimeZone Basics
A TimeZone represents an offset from UTC plus daylight-saving rules. TimeZone.current is the device's zone.
import Foundation
let tz = TimeZone.current
print("Identifier present: \(!tz.identifier.isEmpty)")Creating a Specific Zone
Construct a named zone with TimeZone(identifier:), for example "America/New_York" or "Europe/Istanbul".
import Foundation
if let tokyo = TimeZone(identifier: "Asia/Tokyo") {
print("Tokyo offset seconds: \(tokyo.secondsFromGMT())")
}Offsets from GMT
secondsFromGMT(for:) gives the current offset, which can change across the year due to daylight saving.
import Foundation
let utc = TimeZone(identifier: "UTC")!
print("UTC offset is zero: \(utc.secondsFromGMT() == 0)")Zones Affect Calendars
Set a calendar's timeZone so that day and hour calculations use that region instead of the device default.
import Foundation
var cal = Calendar.current
cal.timeZone = TimeZone(identifier: "UTC")!
let hour = cal.component(.hour, from: Date())
print("Hour in UTC between 0 and 23: \(0...23 ~= hour)")Zones in Formatting
A Date.FormatStyle can carry a specific time zone so output reflects that region regardless of device settings.
import Foundation
let style = Date.FormatStyle(date: .omitted, time: .shortened)
.timeZone(TimeZone(identifier: "UTC")!)
let s = Date().formatted(style)
print(!s.isEmpty)Locale Basics
A Locale captures language and regional conventions: month names, first day of week, number and date ordering.
import Foundation
let locale = Locale.current
print("Has identifier: \(!locale.identifier.isEmpty)")Creating a Locale
Build a specific locale with Locale(identifier:), such as "fr_FR" or "tr_TR".
import Foundation
let french = Locale(identifier: "fr_FR")
print("French locale id: \(french.identifier)")Locale Changes Output
Applying a locale to a format style changes month names and ordering. The same date reads differently in each language.
import Foundation
let now = Date()
let style = Date.FormatStyle(date: .long, time: .omitted)
.locale(Locale(identifier: "en_US"))
print(!now.formatted(style).isEmpty)Locale and Calendar Together
Locale also affects the calendar's first weekday and week numbering. Combine zone, locale, and calendar for fully correct results.
import Foundation
var cal = Calendar(identifier: .gregorian)
cal.locale = Locale(identifier: "en_US")
print("First weekday: \(cal.firstWeekday)")Storing UTC, Displaying Local
Best practice: store and transmit dates in UTC, then convert to the user's zone and locale only for display.
import Foundation
let instant = Date()
let utcStyle = Date.FormatStyle(date: .numeric, time: .standard)
.timeZone(TimeZone(identifier: "UTC")!)
print("UTC display ready: \(!instant.formatted(utcStyle).isEmpty)")Quick Check
What is the recommended way to handle dates across regions?
Recap
TimeZone sets the UTC offset and DST rules, while Locale controls language and regional formatting. Both attach to calendars and format styles. Store dates in UTC and convert to local zone and locale only when displaying. That completes the dates and calendars course.
Frequently asked questions
Is the “Time Zones and Locales” lesson free?
Yes — the full text of “Time Zones and Locales” 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 “Time Zones and Locales”?
Handle zone and locale differences correctly. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time Zones and Locales” 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.
All lessons in this course
- The Date Type and Time Intervals
- Calendar and DateComponents
- Formatting Dates
- Time Zones and Locales