这是检查 serverOnNowDate 是否在publishedStartTime 和publishedEndTime 之间的好方法吗?
return !( [self.publishedStartTime compare:serverOnNowDate] == NSOrderedDescending
|| [self.publishedEndTime compare:serverOnNowDate] == NSOrderedAscending );
写成实例方法:
- (BOOL)isBetweenDate:(NSDate *)earlierDate andDate:(NSDate *)laterDate
{
// first check that we are later than the earlierDate.
if ([self compare:earlierDate] == NSOrderedDescending) {
// next check that we are earlier than the laterData
if ( [self compare:laterDate] == NSOrderedAscending ) {
return YES;
}
}
// otherwise we are not
return NO;
}
用法:
NSDate *date = [NSDate now];
if ( [date isBetween:startDate andDate:finishDate] ) {
NSLog(@"Between dates!!!");
}
You can use the below method
- (BOOL)inputDate:(NSDate*)date start:(NSDate*)beginDate end:(NSDate*)endDate
{
if ([date compare:beginDate] == NSOrderedAscending)
return NO;
if ([date compare:endDate] == NSOrderedDescending)
return NO;
return YES;
}