Working with Dates and Times in Swift: A Complete Guide

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

Introduction to Working with Dates and Time in Swift

Working with dates and time is one of the key tasks in developing applications for iOS and macOS. Whether it's displaying the current date, calculating the difference between events, or scheduling notifications, Swift provides a powerful and flexible toolkit for these purposes. In this article, we will cover the main classes and methods: Date, DateFormatter, Calendar, DateComponents, and TimeInterval. You will learn how to create, format, compare, and manipulate dates in Swift.

The material will be useful for both beginners and experienced developers looking to systematize their knowledge. All code examples can be used immediately in your projects.



Main Classes for Working with Dates



The Date Class

Date is a fundamental type representing a specific moment in time, independent of time zone or calendar. It stores the number of seconds that have passed since January 1, 2001 (the Cocoa epoch).

import Foundation

// Current date and timelet now = Date()print("Now: \\(now)")

// Date 1 hour from nowlet oneHourLater = Date(timeIntervalSinceNow: 3600)print("In one hour: \\(oneHourLater)")

// Date from a given number of seconds since the epochlet epochDate = Date(timeIntervalSinceReferenceDate: 0)print("Cocoa epoch: \\(epochDate)")


TimeInterval

TimeInterval is simply an alias for Double, representing a number of seconds. It is used to specify time intervals.

let interval: TimeInterval = 86400 // 24 hours in secondslet tomorrow = Date(timeIntervalSinceNow: interval)print("Tomorrow: \\(tomorrow)")


DateFormatter

DateFormatter is responsible for converting a date to a string and back. It is one of the most frequently used classes.

let formatter = DateFormatter()formatter.dateStyle = .longformatter.timeStyle = .short

let currentDate = Date()let dateString = formatter.string(from: currentDate)print("Formatted date: \\(dateString)")

// Reverse conversionlet dateFromString = formatter.date(from: "April 15, 2025 at 2:30 PM")print("Date from string: \\(dateFromString ?? Date())")

You can set a custom format using dateFormat:

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"let customString = formatter.string(from: Date())print("Custom format: \\(customString)")


Calendar and Date Components



Calendar

Calendar allows you to work with dates in the context of a specific calendar (Gregorian, Islamic, etc.) and time zone.

let calendar = Calendar.currentlet components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: Date())print("Year: \\(components.year!), Month: \\(components.month!), Day: \\(components.day!)")


DateComponents

DateComponents is a structure containing individual date components (year, month, day, hour, etc.). It is used to create new dates or extract parts from existing ones.

// Creating a date from componentsvar birthdayComponents = DateComponents()birthdayComponents.year = 1990birthdayComponents.month = 6birthdayComponents.day = 15birthdayComponents.hour = 10

if let birthday = calendar.date(from: birthdayComponents) { print("Birthday: \\(birthday)")}

// Adding days to a dateif let nextWeek = calendar.date(byAdding: .day, value: 7, to: Date()) { print("In one week: \\(nextWeek)")}

Components can be used to check date properties:

let isToday = calendar.isDateInToday(Date())print("Is today? \\(isToday)")

let isWeekend = calendar.isDateInWeekend(Date())print("Is weekend? \\(isWeekend)")


Comp

Blogs

Book Recommendations