I want to get the current week number and display it in my app. There are many examples online using the old WeekOfYearCalendarUnit, which would have solved my problem, but in true Apple fashion it's been deprecated and no alternative is offered. Iv'e managed to pull this function off that atleast display's the weeks of the year. But not the current one.
func weekYear(){
let weekRange = NSCalendar.current.range(of: .weekOfYear, in: .yearForWeekOfYear, for: Date())
print("\(weekRange?.count)")
}
My question is how to get the current week number with swift 3 in xcode 8.1?
Try the following code snippet. It is written in Swift 3.
let calendar = Calendar.current
let weekOfYear = calendar.component(.weekOfYear, from: Date(timeIntervalSinceNow: 0))
print(weekOfYear)
This will print the week of year.