The Date Type and Time Intervals
Represent points in time and durations.
The Date Type and Time Intervals is a free Swift Academy lesson on CoddyKit — lesson 1 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.
The Date Type
Foundation's Date represents a single point in time, independent of any calendar or time zone. Internally it is a number of seconds relative to a reference date.
Creating a Date
Date() gives the current instant. Every call returns the moment it executes.
import Foundation
let now = Date()
print("Now is a Date value")
print(type(of: now))Time Intervals
A TimeInterval is simply a Double number of seconds. You add or subtract intervals to shift a date forward or backward.
import Foundation
let now = Date()
let inOneHour = now.addingTimeInterval(3600)
print(inOneHour > now)timeIntervalSince
timeIntervalSince(_:) returns the seconds between two dates as a TimeInterval. Positive means the receiver is later.
import Foundation
let start = Date()
let later = start.addingTimeInterval(120)
let gap = later.timeIntervalSince(start)
print("Gap seconds: \(gap)")Comparing Dates
Date conforms to Comparable, so you can use <, >, and == directly to order events.
import Foundation
let a = Date()
let b = a.addingTimeInterval(10)
print(a < b)
print(a == a)Reference Dates
timeIntervalSinceReferenceDate measures seconds since Jan 1 2001 UTC; timeIntervalSince1970 uses the Unix epoch.
import Foundation
let d = Date(timeIntervalSince1970: 0)
print(d.timeIntervalSince1970)Constructing from Epoch
You can build a specific instant from a Unix timestamp, useful when receiving times from a server.
import Foundation
let epoch = Date(timeIntervalSince1970: 1_700_000_000)
print(epoch.timeIntervalSince1970)Distance Between Dates
The distance(to:) method from Comparable/Strideable also gives the interval between two dates.
import Foundation
let a = Date()
let b = a.addingTimeInterval(45)
print(a.distance(to: b))Past and Future Helpers
The .distantPast and .distantFuture statics give sentinel dates handy as defaults in comparisons.
import Foundation
let now = Date()
print(Date.distantPast < now)
print(Date.distantFuture > now)Measuring Elapsed Time
Capture a start date, do work, then subtract to measure how long something took in seconds.
import Foundation
let start = Date()
var total = 0
for i in 1...1000 { total += i }
let elapsed = Date().timeIntervalSince(start)
print("Sum: \(total)")
print("Elapsed >= 0: \(elapsed >= 0)")Date Is Calendar-Agnostic
Remember: a Date has no concept of year, month, or day. To extract those you need a Calendar, which is the next lesson.
import Foundation
let now = Date()
// To get the year you must use a Calendar, not Date alone.
print("A Date is just an instant in time.")Quick Check
What does laterDate.timeIntervalSince(earlierDate) return?
Recap
Date is a single instant measured in seconds. You shift it with addingTimeInterval, measure gaps with timeIntervalSince, compare with operators, and construct from epochs. It is calendar-agnostic, so the next lesson introduces Calendar and DateComponents.
Frequently asked questions
Is the “The Date Type and Time Intervals” lesson free?
Yes — the full text of “The Date Type and Time Intervals” 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 “The Date Type and Time Intervals”?
Represent points in time and durations. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Date Type and Time Intervals” 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