5

How would I compare only the year-month-day components of 2 NSDates?

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Doom
  • 1,258
  • 3
  • 15
  • 24
  • possible duplicate of [Compare NSDate objects only in terms of year month day](http://stackoverflow.com/questions/3432700/compare-nsdate-objects-only-in-terms-of-year-month-day) – Max MacLeod Aug 30 '13 at 14:53

4 Answers4

11

So here's how you'd do it:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);

NSDate *firstDate = ...; // one date
NSDate *secondDate = ...; // the other date

NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:firstDate];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate:secondDate];

NSDate *truncatedFirst = [calendar dateFromComponents:firstComponents];
NSDate *truncatedSecond = [calendar dateFromComponents:secondComponents];

NSComparisonResult result = [truncatedFirst compare:truncatedSecond];
if (result == NSOrderedAscending) {
  //firstDate is before secondDate
} else if (result == NSOrderedDescending) {
  //firstDate is after secondDate
}  else {
  //firstDate is the same day/month/year as secondDate
}

Basically, we take the two dates, chop off their hours-minutes-seconds bits, and the turn them back into dates. We then compare those dates (which no longer have a time component; only a date component) and see how they compare to eachother.

WARNING: typed in a browser and not compiled. Caveat implementor

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
4

Check out this topic NSDate get year/month/day

Once you pull out the day/month/year, you can compare them as integers.

If you don't like that method

Instead, you could try this..

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy"];
int year = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"MM"];
int month = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"dd"];
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];
  • And another way...

    NSDateComponents *dateComp = [calendar components:unitFlags fromDate:date];
    
    NSInteger year = [dateComp year];
    
    NSInteger month = [dateComp month];
    
    NSInteger day = [dateComp day];
    
Community
  • 1
  • 1
Kyle Uithoven
  • 2,414
  • 5
  • 30
  • 43
1

Since iOS 8 you can use -compareDate:toDate:toUnitGranularity: method of NSCalendar.

Like this:

    NSComparisonResult comparison = [[NSCalendar currentCalendar] compareDate:date1 toDate:date2 toUnitGranularity:NSCalendarUnitDay];
tagirkaZ
  • 469
  • 7
  • 19
  • This should be the accepted answer for iOS 8 or later. Makes fewer assumptions and is more concise than the other answers. – ndmeiri Sep 18 '19 at 16:28
-1

With the the -[NSDate compare:] method - NSDate Compare Reference

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Mahesh
  • 34,573
  • 20
  • 89
  • 115