Question

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

Was it helpful?

Solution

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

OTHER TIPS

Try

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

if (components.hour==0&&components.minute==0) {
    NSLog(@"Equal time");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top