Swift – Find Next Occurrence of a given Weekday

In this tutorial, we will learn how to find the next occurrence of a given weekday in Swift. We will use the Calendar class to compute the next date that matches the desired weekday, starting from today.


Finding the Next Occurrence of a Weekday

In Swift, the Calendar class provides a method called nextDate(after:matching:matchingPolicy:) that allows us to find the next occurrence of a specific weekday. This method is flexible and can handle various date computations.

Here’s an example to find the next occurrence of a given weekday:

</>
Copy
import Foundation

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

// Define the weekday (1 = Sunday, 2 = Monday, ..., 7 = Saturday)
let targetWeekday = 5 // Thursday

if let nextThursday = calendar.nextDate(after: today, matching: DateComponents(weekday: targetWeekday), matchingPolicy: .nextTime) {
    print("Next Thursday: \(nextThursday)")
}

Explanation:

  • DateComponents(weekday: 5): Specifies the desired weekday (e.g., 5 for Thursday).
  • nextDate(after:matching:matchingPolicy:): Finds the next date that matches the specified components.
  • .nextTime: Ensures the search continues until the next match is found.

Formatting the Result

To display the result in a human-readable format, use the DateFormatter class:

</>
Copy
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full

if let nextThursday = calendar.nextDate(after: today, matching: DateComponents(weekday: targetWeekday), matchingPolicy: .nextTime) {
    let formattedDate = dateFormatter.string(from: nextThursday)
    print("Next Thursday (formatted): \(formattedDate)")
}

In this example, the date is formatted as a full date (e.g., Thursday, November 23, 2024).


Function to Find the Next Occurrence

You can encapsulate the logic into a reusable function:

</>
Copy
func findNextOccurrence(of weekday: Int, from date: Date = Date()) -> Date? {
    let calendar = Calendar.current
    return calendar.nextDate(after: date, matching: DateComponents(weekday: weekday), matchingPolicy: .nextTime)
}

// Example usage
if let nextMonday = findNextOccurrence(of: 2) { // Monday is 2
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .full
    print("Next Monday: \(dateFormatter.string(from: nextMonday))")
}

This function simplifies the process of finding the next occurrence of any weekday.

Complete Swift Program

Here’s the complete Swift program to find the next occurrence of a given weekday:

main.swift

</>
Copy
import Foundation

// Function to find the next occurrence of a given weekday
func findNextOccurrence(of weekday: Int, from date: Date = Date()) -> Date? {
    let calendar = Calendar.current
    return calendar.nextDate(after: date, matching: DateComponents(weekday: weekday), matchingPolicy: .nextTime)
}

// Example usage
let today = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full

if let nextThursday = findNextOccurrence(of: 5) { // Thursday is 5
    print("Today: \(dateFormatter.string(from: today))")
    print("Next Thursday: \(dateFormatter.string(from: nextThursday))")
}

if let nextMonday = findNextOccurrence(of: 2) { // Monday is 2
    print("Next Monday: \(dateFormatter.string(from: nextMonday))")
}

Output:

Today: Thursday, 21 November 2024
Next Thursday: Thursday, 28 November 2024
Next Monday: Monday, 25 November 2024
Program ended with exit code: 0

This program calculates the next occurrence of any specified weekday and displays it in a formatted style.

Xcode Screenshot

Swift Program to Find Next Occurrence of a given Weekday