Working with Dates in Swift: Foundation, SwiftDate, DateTools (iOS/macOS)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Dates in Swift (iOS/macOS): Library Overview



In iOS and macOS development, working with dates and times is one of the key tasks. The standard Foundation framework provides powerful but sometimes cumbersome tools. To simplify code and improve readability, developers often use third-party libraries. In this article, we will look at native Swift (Foundation) capabilities and two popular libraries: SwiftDate and DateTools.



1. Foundation: Built-in Capabilities



The basic Foundation toolkit includes Date, DateFormatter, Calendar, and TimeZone. This is sufficient for most tasks but requires verbose code.



Example: Creating and Formatting a Date

import Foundation

let now = Date()let formatter = DateFormatter()formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"formatter.timeZone = TimeZone(abbreviation: "UTC")let dateString = formatter.string(from: now)print(dateString) // 2025-04-01 14:30:00


Example: Calculating the Difference Between Dates

let startDate = Date()let endDate = Calendar.current.date(byAdding: .day, value: 7, to: startDate)!let components = Calendar.current.dateComponents([.day, .hour], from: startDate, to: endDate)print("\\(components.day!) days, \\(components.hour!) hours")


Pros of Foundation: no dependencies, full control, support for all time zones and calendars.
Cons: lots of code for simple operations, complexity with localization and relative dates.



2. SwiftDate — Powerful and Flexible Library



SwiftDate is one of the most popular libraries for working with dates in Swift. It extends Foundation's capabilities by adding convenient methods for parsing, formatting, manipulations, and working with time zones.



Installation (Swift Package Manager)

// Package.swiftdependencies: [    .package(url: "https://github.com/malcommac/SwiftDate.git", from: "6.3.0")]


Example: Creating a Date from a String

import SwiftDate

let date = "2025-04-01T14:30:00+0000".toDate()print(date?.toFormat("dd.MM.yyyy HH:mm")) // 01.04.2025 14:30


Example: Relative Dates and Intervals

let now = Date()let oneWeekAgo = now - 1.weeksprint(oneWeekAgo.toRelative()) // "1 week ago"

let nextMonth = now + 1.monthsprint(nextMonth.toFormat("MMMM yyyy")) // May 2025


Example: Working with Time Zones

let parisDate = DateInRegion(Date(), region: .init(calendar: Calendars.gregorian, zone: Zones.europeParis, locale: Locales.french))print(parisDate.toFormat("HH:mm")) // time in Paris


Pros of SwiftDate: concise syntax, built-in support for relative dates, regions and locales, powerful parsing.
Cons: large library size (~2 MB), may be overkill for simple projects.



3. DateTools — Simplicity and Readability



DateTools is a lightweight library focused on code readability. It adds methods to Date for quick interval calculation, comparison, and formatting.



Installation (CocoaPods)

pod 'DateToolsSwift'


Example: Checking Time Intervals

import DateToolsSwift

let date1 = Date()let date2 = date1 + 2.hoursprint(date2.isToday) // trueprint(date1.earlierDate(date2) == date1) // true


Example: Human-Readable Strings

let pastDate = Date() - 3.daysprint(pastDate.timeAgoSinceNow) // "3 days ago"

let futureDate = Date() + 1.weeksprint(futureDate.timeAgoSinceNow) // "in 1 w

Recommendations