How to calculate the week number for a given date in Swift

1 Answer

0 votes
import Foundation

let date = Date()

// Get the current calendar
let calendar = Calendar.current

// Extract the week of the year from the date
if let weekOfYear = calendar.dateComponents([.weekOfYear], from: date).weekOfYear {
    print("Week number: \(weekOfYear)")
} else {
    print("Could not calculate the week number.")
}



/*
run:

Week number: 11

*/

 



answered Mar 15, 2025 by avibootz
...