简体   繁体   中英

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?

NSDateComponentNSDate上使用compare: NSDateComponent

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM