这是检查serverOnNowDate是否在publishedStartTime和publishedEndTime之间的好方法吗?
return !( [self.publishedStartTime compare:serverOnNowDate] == NSOrderedDescending
|| [self.publishedEndTime compare:serverOnNowDate] == NSOrderedAscending );
Run Code Online (Sandbox Code Playgroud)
写成实例方法:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
用法:
NSDate *date = [NSDate now];
if ( [date isBetween:startDate andDate:finishDate] ) {
NSLog(@"Between dates!!!");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3291 次 |
| 最近记录: |