Working with Dates in Swift (iOS/macOS): Libraries and Frameworks

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): Overview of Libraries and Tools



In the Apple ecosystem, working with dates and time is traditionally implemented through the Foundation framework. However, for complex scenarios (parsing non-standard formats, working with relative dates, manipulations in different time zones), developers often turn to third-party libraries. In this article, we will look at both the built-in capabilities of Swift and popular tools for date processing.



1. Built-in Foundation Tools (Date, DateFormatter, Calendar)



The basic type for working with dates is the Date structure. It represents an absolute moment in time (the number of seconds since January 1, 2001). DateFormatter is used for formatting, and Calendar is used for calendar calculations.



import Foundation

// Getting the current datelet now = Date()print("Current date: \\(now)")

// Date formattinglet formatter = DateFormatter()formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"formatter.timeZone = TimeZone(identifier: "Europe/Moscow")let formattedDate = formatter.string(from: now)print("Formatted date: \\(formattedDate)")

// Parsing a string into a datelet dateString = "2025-03-15 14:30:00"if let parsedDate = formatter.date(from: dateString) { print("Parsed date: \\(parsedDate)")}

// Calendar calculationslet calendar = Calendar.currentlet tomorrow = calendar.date(byAdding: .day, value: 1, to: now)!print("Tomorrow: \\(tomorrow)")


2. SwiftDate — A Powerful Library for Working with Dates



SwiftDate is the most popular third-party library for Swift. It extends the capabilities of Foundation by adding:

  • Convenient syntax for creating dates (e.g., DateInRegion)
  • Working with relative dates ("2 hours ago", "in 3 days")
  • Support for all time zones and calendars
  • Parsing strings in any format


import SwiftDate

// Creating a date in a regionlet region = Region(calendar: Calendars.gregorian, zone: Zones.europeMoscow, locale: Locales.russian)let date = DateInRegion("2025-03-15 14:30:00", format: "yyyy-MM-dd HH:mm:ss", region: region)!print("Date in region: \\(date)")

// Relative timelet now = DateInRegion()let twoHoursAgo = now - 2.hoursprint("2 hours ago: \\(twoHoursAgo.toRelative())") // "2 hours ago"

// Difference between dateslet later = now + 5.dayslet difference = later - nowprint("Difference: \\(difference.days) days")

// Conversion to another time zonelet newYorkRegion = Region(zone: Zones.americaNewYork)let dateInNY = date.convert(to: newYorkRegion)print("In New York: \\(dateInNY)")


3. TimeInterval and Working with Time Intervals



TimeInterval is a data type in Swift representing a number of seconds. It is often used to measure time spans and delays.



import Foundation

// Creating an intervallet oneHour: TimeInterval = 3600let twoDays: TimeInterval = 2 * 24 * 3600

// Measuring execution timelet start = Date()// Simulating a long operationsleep(2)let end = Date()let elapsed = end.timeIntervalSince(start)print("Elapsed \\(elapsed) seconds")

// Using with DispatchQueueDispatchQueue.main.asyncAfter(deadline: .now() + oneHour) { print("One hour passed")}


4. ISO8601DateFormatter — Working with International Formats



For working with dates in ISO 8601 format (often used in APIs), Apple provides a specialized class ISO8601DateFormatter.



import Foundation

let isoFormatter = ISO8601DateFormatter()isoFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]

// Parsing an ISO stringlet isoString = "2025-03-15T14:30:00.000Z"if let date =

Recommendations