Aneel Aneel - 27 days ago 9
Swift Question

How do I find the beginning of the week from an NSDate?

I'm implementing a calendar view, and I'd like it to start at the beginning of the week containing a particular date. Eg. If the target date is Monday, Feb 29, 2016, and the current calendar is set to start on Sunday, I'd like my view to start with Sunday, February 28.

This seems like it should be straightforward:

let calendar = NSCalendar.currentCalendar()
let firstDate = calendar.nextDateAfterDate(targetDate,
matchingUnit: .Weekday,
value: calendar.firstWeekday,
options: .SearchBackwards)


But this fails with:


Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Exactly one option from the set {NSCalendarMatchPreviousTimePreservingSmallerUnits, NSCalendarMatchNextTimePreservingSmallerUnits, NSCalendarMatchNextTime} must be specified.'


I can get basically what I want with:

let firstDate = calendar.nextDateAfterDate(firstDate,
matchingUnit: .Weekday,
value: calendar.firstWeekday,
options: .MatchPreviousTimePreservingSmallerUnits)?
.dateByAddingTimeInterval(-7 * 84600)


But it seems like a bad practice, since sometimes the number of seconds in a day isn't 86400.

Is there a better way?

Answer

you can use NSCalendar method dateFromComponents passing .YearForWeekOfYear, .WeekOfYear components from any date it will return the first day of the week from the calendar used. So if you would like to get Sunday just use Gregorian calendar. if you would like to get the Monday as the first day of the week you can use NSCalendar iso8601 as you can see in this answer

extension NSDate {
    struct Gregorian {
        static let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    }
    var startOfWeek: NSDate {
        return Gregorian.calendar.dateFromComponents(Gregorian.calendar.components([.YearForWeekOfYear, .WeekOfYear ], fromDate: self))!
    }
}

Xcode 8.1 • Swift 3.0.1

extension Date {
    struct Gregorian {
        static let calendar = Calendar(identifier: .gregorian)
    }
    var startOfWeek: Date? {
        return Gregorian.calendar.date(from: Gregorian.calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
    }
}

usage:

Date().startOfWeek   // "Nov 6, 2016, 12:00 AM"
Comments