简体   繁体   English

比较两个nsdatecomponent

[英]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. 如果我有两个nsdatecomponent,我想知道第一个对象中的TIME是否大于第二个。

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: 正确(和唯一)比较“动态”两个NSDateComponents的方法是使用NSCalendar对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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