Working with Dates in Swift (iOS/macOS): Library Overview
Working with dates and time is one of the most common and simultaneously complex tasks in iOS and macOS development. The standard Foundation framework provides powerful but not always intuitive tools: Date, DateFormatter, Calendar, TimeInterval. However, to simplify routine operations (string parsing, human-readable intervals, localization), specialized libraries exist. In this article, we will review the main approaches and popular tools.
1. Built-in Foundation Tools
Before integrating third-party solutions, it is important to know how to work with native tools. Main classes:
Date— a point in time (with second precision).DateFormatter— converting a string to a date and back.Calendar— working with calendar units (year, month, day).TimeInterval— the difference between dates in seconds.
Example: Date Formatting
import Foundation
let now = Date()let formatter = DateFormatter()formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"formatter.locale = Locale(identifier: "ru_RU")let dateString = formatter.string(from: now)print(dateString) // 2025-02-18 14:30:00Example: Calculating the Difference in Days
let calendar = Calendar.currentlet start = Date()let end = calendar.date(byAdding: .day, value: 7, to: start)!let components = calendar.dateComponents([.day], from: start, to: end)print(components.day!) // 7These capabilities are sufficient for most basic scenarios. But as soon as complex parsing or localization is required, the code becomes cumbersome.
2. DateHelper Library
DateHelper is a lightweight library (available via Swift Package Manager) that adds convenient extensions to Date. The main feature is human-readable relative dates.
Installation: in Package.swift, add the dependency https://github.com/melvitax/DateHelper.
Example: "3 hours ago"
import DateHelper
let pastDate = Date() - 3.hoursprint(pastDate.toRelativeString()) // "3 hours ago"// With localization: print(pastDate.toRelativeString(locale: "ru"))The library supports many output formats: .short, .medium, .full. This is an ideal option for applications that need to display "just now", "yesterday", "2 weeks ago" without writing a custom parser.
3. SwiftDate — The Most Powerful Tool
SwiftDate (author: Daniele Margutti) is effectively the de facto standard for working with dates in Swift. It includes:
- String parsing with auto-format detection.
- Date manipulations (adding/subtracting components).
- Calculating differences in any units.
- Localization (support for 30+ languages).
- Working with time zones and calendars.
Installation: via SPM or CocoaPods.
Example: Parsing a String with an Unknown Format
import SwiftDate
let dateString = "2025-02-18T14:30:00+03:00"if let date = dateString.toDate() { print(date.year) // 2025 print(date.month) // 2 print(date.toFormat("dd MMMM yyyy", locale: Locales.russian)) // "18 февраля 2025"}Example: Getting an Interval in a Human-Readable Form
let start = DateInRegion(Date())let end = start + 2.hours + 30.minuteslet interval = start.toRelativeString(end, style: RelativeFormatter.defaultStyle())print(interval) // "2 hours 30 minutes later"SwiftDate can also work with Also in library
Recommendations