alex
alex

Reputation: 879

Compare two nsdatecomponent

if I have two nsdatecomponent and I want a know if the TIME in the first object is bigger than the second.

example:

if (nsdatecomponentObject1 > nsdatecomponentObject2)
{
    //something
}

Because this way don't works, how I can do this?

Upvotes: 4

Views: 4345

Answers (2)

valvoline
valvoline

Reputation: 8117

The right (and only) way to compare "on-the-fly" two NSDateComponents, is to use an NSCalendar object:

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

switch([[gregorian dateFromComponents:oneNSDateComponents] compare:[gregorian dateFromComponents:anotherNSDateComponents]]) {
    case NSOrderedSame:
        NSLog(@"orderSame");
        break;
    case  NSOrderedAscending:
        NSLog(@"orderAscending");
        break;
    case  NSOrderedDescending:
        NSLog(@"orderDescending");
        break;
}

Upvotes: 1

Jacob Relkin
Jacob Relkin

Reputation: 163268

Use compare: on the NSDate of the NSDateComponent.

if([[nsdatecomponentObject1 date] compare:[nsdatecomponentObject2 date]] == NSOrderedAscending)

Upvotes: 7

Related Questions