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. This is lesson 1 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.
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.
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 “The Date Type and Time Intervals” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “The Date Type and Time Intervals”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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