2

这是检查 serverOnNowDate 是否在publishedStartTime 和publishedEndTime 之间的好方法吗?

 return !( [self.publishedStartTime compare:serverOnNowDate] == NSOrderedDescending
        || [self.publishedEndTime compare:serverOnNowDate] == NSOrderedAscending );
4

2 回答 2

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!!!");
}
于 2013-11-25T14:14:48.323 回答
2

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;
}
于 2013-11-06T09:55:11.493 回答