0

I want to compare 2 specific days, but, i want to compare month and year, not days. For example, i got one date something like 2016-08-16, and other date like 2016-08-10, i need to compare dates and got equality, because their month are equal.

Therefore, i need a way to make both NSDates with equal days, so i need to make 2016-08-10 to be 2016-08-00. After that i can modify second date as well and make comparison.

I have simple code snipper for equality, but i suppose it consider days and hours, etc. as well:

 if ([dateOld compare:dateNew] == NSOrderedDescending) {
        NSLog(@"date1 is later than date2");
    } else if ([dateOld compare:dateNew] == NSOrderedAscending) {
        NSLog(@"date1 is earlier than date2");
    } else {
        NSLog(@"dates are the same");
    }

To make it work i need to "zero" days and time of NSDates. Is there any way to do that?

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 1
    `NSDateComponents` class reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/#//apple_ref/doc/uid/TP40001502-SW1 – Robotic Cat Aug 03 '16 at 15:22
  • 1
    `NSDateComponents`, but also `NSCalendar`, with `compareDate:toDate:toUnitGranularity:` method could be what you are looking for. – Larme Aug 03 '16 at 15:25
  • @Larme could you provide an example with that? – Evgeniy Kleban Aug 03 '16 at 15:27
  • I think that a quick search with the key words I gave (Class names, method name) and "compare" should give you quick example on SO like this one: http://stackoverflow.com/questions/1854890/comparing-two-nsdates-and-ignoring-the-time-component – Larme Aug 03 '16 at 15:29
  • @Larme okay, thank you. – Evgeniy Kleban Aug 03 '16 at 15:30

2 Answers2

2

See this SO answer: Comparing certain components of NSDate?

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (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 month/year as secondDate
}
Community
  • 1
  • 1
A O
  • 5,516
  • 3
  • 33
  • 68
1

If you have two dates,

NSDate *now = // some date
NSDate *other = // some other date

You can do this comparison as:

if ([[NSCalendar currentCalendar] compareDate:now toDate:other toUnitGranularity:NSCalendarUnitMonth] == NSOrderedSame) {
    NSLog(@"Same month");
} else {
    NSLog(@"Different month");
}
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170