Swift – Add Days to a Given Date

In this tutorial, we will learn how to add days to a given date in Swift. We will use the Calendar class and the adding method to perform date arithmetic. This tutorial includes examples to add days, format the resulting date, and handle edge cases.


Adding Days to a Given Date in Swift

To add days to a date in Swift, we use the Calendar class. The Calendar class provides the date(byAdding:to:) method, which lets us add or subtract specific components (such as days, months, or years) from a given date.

Here’s an example to add 5 days to the current date:

</>
Copy
import Foundation

let currentDate = Date()
let calendar = Calendar.current

if let newDate = calendar.date(byAdding: .day, value: 5, to: currentDate) {
    print("Current Date: \(currentDate)")
    print("New Date after adding 5 days: \(newDate)")
}

In this example, the date(byAdding:to:) method adds 5 days to the current date and returns the resulting date.


Formatting the Resulting Date

To display the resulting date in a human-readable format, use the DateFormatter class. Here’s how you can format the new date:

</>
Copy
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

if let newDate = calendar.date(byAdding: .day, value: 5, to: currentDate) {
    let formattedDate = dateFormatter.string(from: newDate)
    print("Formatted New Date: \(formattedDate)")
}

Explanation:

  • dateFormatter.dateFormat = "yyyy-MM-dd": Sets the desired format for the date (e.g., 2024-11-24).
  • dateFormatter.string(from: newDate): Converts the Date object into a string using the specified format.

Adding Negative Days

You can also subtract days by passing a negative value to the date(byAdding:to:) method. For example, to subtract 7 days from the current date:

</>
Copy
if let newDate = calendar.date(byAdding: .day, value: -7, to: currentDate) {
    print("New Date after subtracting 7 days: \(newDate)")
}

In this case, the resulting date will be 7 days earlier than the current date.


Complete Swift Program

Here’s the complete Swift program to add and subtract days from a given date:

main.swift

</>
Copy
import Foundation

let currentDate = Date()
let calendar = Calendar.current

// Add 5 days to the current date
if let newDate = calendar.date(byAdding: .day, value: 5, to: currentDate) {
    print("Current Date: \(currentDate)")
    print("New Date after adding 5 days: \(newDate)")
    
    // Format the new date
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let formattedDate = dateFormatter.string(from: newDate)
    print("Formatted New Date: \(formattedDate)")
}

// Subtract 7 days from the current date
if let newDate = calendar.date(byAdding: .day, value: -7, to: currentDate) {
    print("New Date after subtracting 7 days: \(newDate)")
    
    // Format the new date
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let formattedDate = dateFormatter.string(from: newDate)
    print("Formatted New Date after subtracting 7 days: \(formattedDate)")
}

Output:

Current Date: 2024-11-21 00:33:59 +0000
New Date after adding 5 days: 2024-11-26 00:33:59 +0000
Formatted New Date: 2024-11-26
New Date after subtracting 7 days: 2024-11-14 00:33:59 +0000
Formatted New Date after subtracting 7 days: 2024-11-14
Program ended with exit code: 0

Xcode Screenshot:

Swift Program to Add Days to a Given Date