0Pricing
Swift Academy · Lesson

Calendar and DateComponents

Do date arithmetic across calendars.

Calendar and DateComponents is a free Swift Academy lesson on CoddyKit — lesson 2 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.

Calendar and DateComponents

A Calendar interprets a Date into human units like year, month, and day. DateComponents holds those broken-out values.

The Current Calendar

Calendar.current reflects the user's settings. It is the gateway between raw Date instants and named fields.

import Foundation

let cal = Calendar.current
print("Calendar identifier: \(cal.identifier)")

Extracting Components

dateComponents(_:from:) pulls the requested fields out of a date into a DateComponents value.

import Foundation

let cal = Calendar.current
let comps = cal.dateComponents([.year, .month, .day], from: Date())
print("Year is set: \(comps.year != nil)")

Reading a Single Field

The component(_:from:) shortcut returns one field directly as an Int.

import Foundation

let cal = Calendar.current
let weekday = cal.component(.weekday, from: Date())
print("Weekday number between 1 and 7: \(1...7 ~= weekday)")

Building a Date from Components

Fill a DateComponents and ask the calendar to produce the matching Date with date(from:).

import Foundation

var comps = DateComponents()
comps.year = 2024
comps.month = 12
comps.day = 25
let cal = Calendar.current
if let date = cal.date(from: comps) {
    print("Built a date: \(date.timeIntervalSince1970 > 0)")
}

Date Arithmetic

date(byAdding:to:) adds a component amount safely, handling month lengths and leap years for you.

import Foundation

let cal = Calendar.current
let now = Date()
if let nextWeek = cal.date(byAdding: .day, value: 7, to: now) {
    print(nextWeek > now)
}

Adding Multiple Units

You can add several units at once by passing a DateComponents describing the offset.

import Foundation

let cal = Calendar.current
var offset = DateComponents()
offset.month = 1
offset.day = 3
if let future = cal.date(byAdding: offset, to: Date()) {
    print(future > Date())
}

Differences Between Dates

Reverse the operation: dateComponents(_:from:to:) returns the elapsed units between two dates.

import Foundation

let cal = Calendar.current
let now = Date()
let later = now.addingTimeInterval(3 * 86400)
let diff = cal.dateComponents([.day], from: now, to: later)
print("Days apart: \(diff.day ?? -1)")

Start of Day

startOfDay(for:) returns midnight of the date in the calendar, handy for grouping events by day.

import Foundation

let cal = Calendar.current
let midnight = cal.startOfDay(for: Date())
print(midnight <= Date())

Checking Same Day

isDate(_:inSameDayAs:) tells you whether two dates fall on the same calendar day.

import Foundation

let cal = Calendar.current
let now = Date()
let soon = now.addingTimeInterval(60)
print(cal.isDate(now, inSameDayAs: soon))

Why Use Calendar

Doing arithmetic by adding raw seconds is wrong across DST and month boundaries. Always use Calendar for day/month/year math.

import Foundation

let cal = Calendar.current
// Correct: add 1 month, not 30 * 86400 seconds.
if let d = cal.date(byAdding: .month, value: 1, to: Date()) {
    print("Added a month correctly: \(d > Date())")
}

Quick Check

Which method correctly adds one month to a date?

Recap

Calendar converts between Date and named fields via DateComponents. You extracted fields, built dates, added units with date(byAdding:), computed differences, and saw why calendar math beats raw second arithmetic. Next: formatting dates for display.

Frequently asked questions

Is the “Calendar and DateComponents” lesson free?

Yes — the full text of “Calendar and DateComponents” 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 “Calendar and DateComponents”?

Do date arithmetic across calendars. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Calendar and DateComponents” 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

  1. The Date Type and Time Intervals
  2. Calendar and DateComponents
  3. Formatting Dates
  4. Time Zones and Locales
← Back to Swift Academy