Search code examples
iosnsdatenscalendar

Cocoa get first day in week


How to get the first day of a week for a date

this seems more easy at it is, since :

  • when the week starts with sunday, i need to get back the sunday date
  • if it starts on monday, i need to get the monday date

the input date is any date in week with time... i tried several approaches, but the edge case make it difficould

i made a function, which however doesn't work in 100% (not sure about the [components setDay: -weekday + 2];)

- (NSDate *)firstDateOfWeek {

    NSCalendar * calendar = [NSCalendar currentCalendar];

    NSDateComponents *weekdayComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
    // because sunday is 0, we need it to be 6
    NSInteger weekday = (([weekdayComponents weekday] + 6) % 7);

    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
    [components setDay: -weekday + 2];

    return [calendar dateFromComponents:components];

}

Solution

  • It is easier to use the rangeOfUnit calendar method, which handles "start of the week" correctly according to the current locale:

    NSDate *date = your date;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *startOfWeek;
    [calendar rangeOfUnit:NSWeekOfYearCalendarUnit
                startDate:&startOfWeek
                 interval:NULL
                  forDate:date];
    

    Using NSDateComponents, it would work as follows (assuming that a week has 7 days):

    NSDate *date = your date;
    NSDateComponents *comp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
                                         fromDate:date];
    NSDate *startOfDay = [calendar dateFromComponents:comp];
    NSInteger diff = (NSInteger)[calendar firstWeekday] - (NSInteger)[comp weekday];
    if (diff > 0)
        diff -= 7;
    NSDateComponents *subtract = [[NSDateComponents alloc] init];
    [subtract setDay:diff];
    NSDate *startOfWeek = [calendar dateByAddingComponents:subtract toDate:startOfDay options:0];