我可以比较日期:设定Date1 = 2011-10-14
和Date2 =2011-10-20
。但是我想实现的是判断15号是否在Date1和Date2之间。代码如下:
if ( [date compare:Date1] == NSOrderedDescending && [date compare:Date2] ==
NSOrderedAscending) { //****** }
但是没有成功,请高手指点如何实现。谢谢!
我可以比较日期:设定Date1 = 2011-10-14
和Date2 =2011-10-20
。但是我想实现的是判断15号是否在Date1和Date2之间。代码如下:
if ( [date compare:Date1] == NSOrderedDescending && [date compare:Date2] ==
NSOrderedAscending) { //****** }
但是没有成功,请高手指点如何实现。谢谢!
试试下面的代码:其中Date1和Date2是判断的范围
NSCalendar *calendar= [NSCalendar currentCalendar];
NSInteger desiredComponents= (NSDayCalendarUnit | NSMonthCalendarUnit);
NSDateComponents *firstComponents= [calendar components:desiredComponents fromDate:Date1];
NSDateComponents *secondComponents= [calendar components:desiredComponents fromDate:Date2];
NSDate *firstWOYear= [calendar dateFromComponents:firstComponents];
NSDate *SecondWOYear = [calendar dateFromComponents:secondComponents];
NSComparisonResult result= [firstWOYear compare:SecondWOYear];
if (result== NSOrderedAscending) {
} else if (result== NSOrderedDescending) {
} else {
}