这是检查serverOnNowDate是否在publishedStartTime和publishedEndTime之间的好方法吗?
return !( [self.publishedStartTime compare:serverOnNowDate] == NSOrderedDescending
|| [self.publishedEndTime compare:serverOnNowDate] == NSOrderedAscending );
答案 0 :(得分:6)
作为实例方法编写:
- (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!!!");
}
答案 1 :(得分:2)
您可以使用以下方法
- (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;
}