簡體   English   中英

比較兩個nsdatecomponent

[英]Compare two nsdatecomponent

如果我有兩個nsdatecomponent,我想知道第一個對象中的TIME是否大於第二個。

例:

if (nsdatecomponentObject1 > nsdatecomponentObject2)
{
    //something
}

因為這種方式不起作用,我怎么能這樣做?

NSDateComponentNSDate上使用compare: NSDateComponent

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

正確(和唯一)比較“動態”兩個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