1

I would like to compare to NSDates but only with regard to hour and minute. The first date is given from an array, the second one is the current. I didn't arrive with this method:

 NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:date
                                                  toDate:currentDate options:0];

Any simple and good idea to solve my problem?

EDIT: User 'sunny' posted some further methods to solve my problem: How to compare two dates in Objective-C

Community
  • 1
  • 1
  • 1
    http://stackoverflow.com/questions/949416/how-to-compare-two-dates-in-objective-c' – Balu May 24 '13 at 12:42

2 Answers2

3
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp1 = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate: date1];

NSDateComponents *comp2 = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate: date2];

Now you have all the data you need to compare the two dates.

comp1.hour > comp2.hour
comp1.minute > comp2.minute
Marcin Kuptel
  • 2,674
  • 1
  • 17
  • 22
1

Try

 NSDateComponents *components = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                              fromDate:date
                                                                toDate:currentDate
                                                               options:0];

if (components.hour==0&&components.minute==0) {
    NSLog(@"Equal time");
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60