Swift – Find Day of Week for a given Date

In this tutorial, we will learn how to find the day of the week for a given date in Swift. We will use the Calendar class and DateFormatter to extract and display the day of the week in a human-readable format.


Finding the Day of the Week

To determine the day of the week for a given date, we use the Calendar class to extract the weekday component. The weekday is represented as an integer where 1 corresponds to Sunday, 2 to Monday, and so on.

Here’s how you can find the weekday for a given date:

</>
Copy
import Foundation

let calendar = Calendar.current

// Define a date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2024-11-19")!

// Get the weekday component
let weekday = calendar.component(.weekday, from: date)
print("Weekday (numeric): \(weekday)")

In this example, the output will be 3, as November 19, 2024, falls on a Tuesday.


Formatting the Day of the Week

To display the day of the week as a string (e.g., Tuesday), use the DateFormatter class:

</>
Copy
dateFormatter.dateFormat = "EEEE"
let dayOfWeek = dateFormatter.string(from: date)
print("Day of the Week: \(dayOfWeek)")

Explanation:

  • EEEE: Represents the full name of the day (e.g., Tuesday).
  • dateFormatter.string(from: date): Converts the Date object into a string with the specified format.

Function to Get Day of Week

You can create a reusable function to get the day of the week for any given date string:

</>
Copy
func getDayOfWeek(from dateString: String, format: String = "yyyy-MM-dd") -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = format
    guard let date = dateFormatter.date(from: dateString) else {
        return nil // Invalid date format
    }
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.string(from: date)
}

// Example usage
if let dayOfWeek = getDayOfWeek(from: "2024-11-19") {
    print("Day of the Week: \(dayOfWeek)")
} else {
    print("Invalid date format")
}

This function takes a date string as input, validates it, and returns the day of the week as a string.


Complete Swift Program

Here’s the complete Swift program to find the day of the week for any given date:

</>
Copy
import Foundation

// Function to get the day of the week for a given date
func getDayOfWeek(from dateString: String, format: String = "yyyy-MM-dd") -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = format
    guard let date = dateFormatter.date(from: dateString) else {
        return nil // Invalid date format
    }
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.string(from: date)
}

// Test cases
let testDates = ["2024-11-19", "2023-01-01", "2022-12-25"]
for date in testDates {
    if let dayOfWeek = getDayOfWeek(from: date) {
        print("Date: \(date), Day of the Week: \(dayOfWeek)")
    } else {
        print("Invalid date format: \(date)")
    }
}

Output:

Date: 2024-11-19, Day of the Week: Tuesday
Date: 2023-01-01, Day of the Week: Sunday
Date: 2022-12-25, Day of the Week: Sunday

This program allows you to determine the day of the week for any valid date and handles different input formats gracefully.