title: Cocoa get first day in week
body: <p>How to get the first day of a week for a date</p>
  
  <p>this seems more easy at it is, since :</p>
  
  <ul>
  <li>when the week starts with sunday, i need to get back the sunday date</li>
  <li>if it starts on monday, i need to get the monday date</li>
  </ul>
  
  <p>the input date is any date in week with time... i tried several approaches, but the edge case make it difficould</p>
  
  <p>i made a function, which however doesn't work in 100% (not sure about the [components setDay: -weekday + 2];)</p>
  
  <pre><code>- (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];
  
  }
  </code></pre>