title: Custom datepicker with UIPickerView first day of the month is always monday
body: <p>I need to create a custom <code>datepicker</code> where first column shows day name, day number and second column shows month name and year.</p>
  
  <p><img src="https://i.stack.imgur.com/2n7vF.png" alt="enter image description here"></p>
  
  <p>Current month works great but for every other month first day of month is always monday. </p>
  
  <pre><code>#pragma Pickerview
  
  -(void)initilazePickerview
  {
  
      //set our picker array data 
      self.yearAndMonth = [[NSMutableArray alloc] init];
      self.day= [[NSMutableArray alloc] init];
      self.backendYearAndMonth= [[NSMutableArray alloc] init];
      self.backendDay= [[NSMutableArray alloc] init];
  
      /*
       next 12 months after current month
       */
  
      NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];
      NSDate           *today           = [NSDate date];
      NSCalendar       *currentCalendar = [NSCalendar currentCalendar];
  
      NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:today];
      int currentMonth = [monthComponents month];
  
      NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:today];
      int currentYear  = [yearComponents year];
      int nextYear     = currentYear + 1;
  
      int months  = 1;
      int year;
      for(int m = currentMonth-1; months &lt;= 12; m++){
  
          int nextMonth = m % 12;
  
          if(nextMonth &lt; currentMonth-1){
              year = nextYear;
          } else {
              year = currentYear;
          }
  
          //NSLog(@"%@ %i",[[dateFormatter shortMonthSymbols] objectAtIndex: nextMonth],year);
          NSString *populateYearAndMonth=[NSString stringWithFormat:@"%@   %i",[[dateFormatter shortMonthSymbols] objectAtIndex: nextMonth],year];
          [self.yearAndMonth addObject:populateYearAndMonth];
  
          //save year and month in array
          NSMutableDictionary *saveYearMonth= [[NSMutableDictionary alloc] init];
          [saveYearMonth setObject:[NSNumber numberWithInt:nextMonth] forKey:@"month"];
          [saveYearMonth setObject:[NSNumber numberWithInt:year] forKey:@"year"];
          [self.backendYearAndMonth addObject:saveYearMonth];
  
          months++;
      }
      [self populateDays:currentMonth-1 year:currentYear];
  
  }
  -(void) populateDays:(int) populateMonth year:(int) populateYear
  {
  
      NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];
      NSDate           *today           = [NSDate date];
      NSCalendar       *currentCalendar = [NSCalendar currentCalendar];
  
      NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:today];
      int currentMonth = [monthComponents month];
  
  
      if (populateMonth==currentMonth-1) {
          NSLog(@"Today");
          [self.day addObject:@"Today"];
          [self.backendDay addObject:today];
  
          NSRange days = [currentCalendar rangeOfUnit:NSDayCalendarUnit
                                 inUnit:NSMonthCalendarUnit
                                forDate:today];
          NSLog(@"days.length = %i",days.length);
  
  
          NSDateComponents *dayComponents = [currentCalendar components:NSDayCalendarUnit fromDate:today];
          int currentDay = [dayComponents day];
  
          for (int i=currentDay+1; i&lt;=days.length; i++) {
              int daySymbol=i%7;
              NSString *populateDay=[NSString stringWithFormat:@"%@   %i",[[dateFormatter shortWeekdaySymbols] objectAtIndex: daySymbol],i];
              [self.day addObject:populateDay];
  
          }
  
      }
      else
      {
          NSDateComponents* comps = [[NSDateComponents alloc] init];
  
          // Set your month here
          [comps setMonth:populateMonth+1];
  
          NSRange days = [currentCalendar rangeOfUnit:NSDayCalendarUnit
                                               inUnit:NSMonthCalendarUnit
                                              forDate:[currentCalendar dateFromComponents:comps]];
          NSLog(@"days.length = %i",days.length);
  
  
          NSDateComponents *dayComponents = [currentCalendar components:NSDayCalendarUnit fromDate:[currentCalendar dateFromComponents:comps]];
          int currentDay = [dayComponents day];
  
          for (int i=currentDay; i&lt;=days.length; i++) {
              int daySymbol=i%7;
              NSString *populateDay=[NSString stringWithFormat:@"%@   %i",[[dateFormatter shortWeekdaySymbols] objectAtIndex: daySymbol],i];
              [self.day addObject:populateDay];
  
          }
  
  
      }
  
       [self.myCustomPicker reloadComponent:0];
  }
      - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  
      if (component == 1)
      {
          [self.day removeAllObjects];
          [self.backendDay removeAllObjects];
          NSDictionary *dataDict= [self.backendYearAndMonth objectAtIndex:row];
          int month=[[dataDict objectForKey:@"month"]intValue];
          int year=[[dataDict objectForKey:@"year"]intValue];
          [self populateDays:month year:year];
  
      }
  }
  // returns the number of 'columns' to display.
  - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  
      NSInteger result = 0;
      if ([pickerView isEqual:self.myCustomPicker]){
          result = 2;
      }
      return result;
  }
  
  // returns the number of rows in each component..
  - (NSInteger)pickerView:(UIPickerView *)pickerView
  numberOfRowsInComponent:(NSInteger)component{
  
      NSInteger result = 0;
      if ([pickerView isEqual:self.myCustomPicker]){
          switch (component) {
              case 0:
                  result = [self.day count];
                  break;
              case 1:
                  result = [self.yearAndMonth count];
                  break;
  
              default:
                  break;
          }
      }
      return result;
  
  }
  
  //return a plain NSString to display the row for the component.
  - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
              forComponent:(NSInteger)component{
      NSString *result = nil;
      if ([pickerView isEqual:self.myCustomPicker]){
          switch (component) {
              case 0:
                  result = [self.day objectAtIndex:row];
                  break;
              case 1:
                  result = [self.yearAndMonth objectAtIndex:row];
                  break;
  
              default:
                  break;
          }
  
      }
      return result;
  }
  </code></pre>
  
  <p>Any advise to get around this problem?
  Thanks Space, </p>