golangphp
golangphp

Reputation: 115

How to check if NSDate is in the specific Month?

I have an array of NSDates and what I am trying to do is check if dates fall in specific month of the year and group them together in another array. I got month and year as NSInteger. It would help me a lot if you guys can help me with the predicate or simple if() statement.

Upvotes: 2

Views: 1795

Answers (1)

codester
codester

Reputation: 37189

You can use below function and make for loop for array.

for(NSDate *date in arrDates){

  if([self isDateFalls:date withYourYear:year andYourMonth:month]){
        NSLog(@"YES DATE FALLS IN YEAR AND MONTH");
  } 
  else{
        NSLog(@"NO DATE NOT FALLS IN YEAR AND MONTH");
  }
}

-(BOOL)isDateFalls:(NSDate *)date withYourYear:(NSInteger)year andYourMonth:(NSInteger)month{

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];
    if(dateComponents.year == year && dateComponents.month == month){

        return YES;
    }

    return NO;

}

Upvotes: 3

Related Questions