iOS判断日期A是否在日期B到日期C之间

方法一: 可以用nsdate 的 timeIntervalSince1970 方法把时间转换成时间戳进行比较,这里timeIntervalSince1970返回的是NSTimeInterval(double)类型,直接比较就可以了

方法二:使用nsdate的compare方法实现

- (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;
    
    if ([date compare:endDate] == NSOrderedDescending)
        return NO;
    
    return YES;
}

方法三:

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 {  

}

  

原文地址:https://www.cnblogs.com/cocoajin/p/3152929.html