,如果我有兩個nsdatecomponent,我想知道,如果在第一個對象的時間比第二大。比較兩個nsdatecomponent
例如:
if (nsdatecomponentObject1 > nsdatecomponentObject2)
{
//something
}
因爲這樣做不工作,我可怎麼辦呢?
,如果我有兩個nsdatecomponent,我想知道,如果在第一個對象的時間比第二大。比較兩個nsdatecomponent
例如:
if (nsdatecomponentObject1 > nsdatecomponentObject2)
{
//something
}
因爲這樣做不工作,我可怎麼辦呢?
在NSDateComponent
的NSDate
上使用compare:
。
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;
}
什麼意思的兩個NSDateComponents比較同一月份,日期和年份是否有指定的工作日,而另一個沒有?這些規則在哪裏指定? – titaniumdecoy 2011-12-23 23:37:12