I have the following code:
getWeek(addDays(Date, amount: 6))
func addDays(date: NSDate, amount: Int) -> NSDate {
let additionalDays = amount
let components = NSDateComponents()
components.day = additionalDays
let futureDate = NSCalendar.currentCalendar()
.dateByAddingComponents(components, toDate: date, options: NSCalendarOptions(rawValue: 0))
return futureDate!
}
func getWeek(today:NSDate)->Int? {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let myComponents = myCalendar.components(.WeekOfYear, fromDate: today)
let weekNumber = myComponents.weekOfYear
return weekNumber
}
Date in getWeek(addDays(Date, amount: 6))returns dec 27, 2015, 7:16PM
This is correct (next Sunday) However, if I try to return the week number for this date, it returns 1, then it should be 53. How can I get the correct week number?
source
share