如何检查限制的date和时间是否与另一个限制重叠?

我想检查date限制* (例如: – 2013年7月22日2:30 PM -22/07/2013 8:30 PM) *是否在另一个时间限制内* (例如: – 2013年7月22日4: 30PM -22/07/2013 6:30 PM) *或不。 我对这个概念完全陌生。 任何人都可以请告诉我如何做到这一点。

任何帮助将不胜感激

下面是我试过的代码:

- (void)viewDidLoad { [self isDateInterval:@"8/1/2013 8:30am" and:@"8/1/2013 8:40am" fallsWithin:@"8/1/2013 8:30am" and:@"8/1/2013 8:55am"]; [super viewDidLoad]; } -(BOOL)isDateInterval:(NSString*)startDate1 and:(NSString*)endDate1 fallsWithin:(NSString*)startDate2 and:(NSString*)endDate2 { BOOL isWithinrange; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"dd/MM/yyyy hh:mmaa"]; NSTimeInterval ti1 = [[formatter dateFromString:startDate1] timeIntervalSince1970]; NSTimeInterval ti2 = [[formatter dateFromString:endDate1] timeIntervalSince1970]; NSTimeInterval ti3 = [[formatter dateFromString:startDate2] timeIntervalSince1970]; NSTimeInterval ti4 = [[formatter dateFromString:endDate2] timeIntervalSince1970]; if( (ti3 > ti1 && ti3 < ti2) || (ti3 < ti1 && ti4 > ti1) ) { //Date with in range isWithinrange=TRUE; } else { isWithinrange=FALSE; //Not with in range } return isWithinrange; } 

我想输出为“isWithin”,因为一个时间间隔的一部分正在进入另一个时间间隔

这段代码会给你你想要的东西。 它检查两个时间间隔的开始date或结束date是否在其他时间间隔的开始date和结束date之间。

 - (BOOL)timeIntervalFromDate:(NSString *)dateString toDate:(NSString *)anotherDateString overlapsWithTimeIntervalFromDate:(NSString *)date2String toDate:(NSString *)anotherDate2String { BOOL isWithinRange = NO; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"dd/MM/yyyy hh:mmaa"; NSDate *date = [formatter dateFromString:dateString]; NSDate *anotherDate = [formatter dateFromString:anotherDateString]; NSDate *date2 = [formatter dateFromString:date2String]; NSDate *anotherDate2 = [formatter dateFromString:anotherDate2String]; NSDate *startDate = [date earlierDate:anotherDate]; NSDate *endDate = [date laterDate:anotherDate]; NSDate *otherStartDate = [date2 earlierDate:anotherDate2]; NSDate *otherEndDate = [date2 laterDate:anotherDate2]; BOOL startDateIsEarlierThanOtherStartDate = [startDate earlierDate:otherStartDate] == startDate; BOOL endDateIsLaterThanOtherStartDate = [endDate laterDate:otherStartDate] == endDate; BOOL startDateIsEarlierThanOtherEndDate = [startDate earlierDate:otherEndDate] == startDate; BOOL endDateIsLaterThanOtherEndDate = [endDate laterDate:otherEndDate] == endDate; BOOL otherStartDateIsEarlierThanStartDate = [startDate earlierDate:otherStartDate] == otherStartDate; BOOL otherEndDateIsLaterThanStartDate = [otherEndDate laterDate:startDate] == otherEndDate; BOOL otherEndDateIsEarlierThanStartDate = [startDate earlierDate:otherEndDate] == otherEndDate; BOOL otherEndDateIsLaterThanEndDate = [endDate laterDate:otherEndDate] == otherEndDate; isWithinRange = (startDateIsEarlierThanOtherStartDate && endDateIsLaterThanOtherStartDate) || (startDateIsEarlierThanOtherEndDate && endDateIsLaterThanOtherEndDate) || (otherStartDateIsEarlierThanStartDate && otherEndDateIsLaterThanStartDate) || (otherEndDateIsEarlierThanStartDate && otherEndDateIsLaterThanEndDate); return isWithinRange; } 

为了说明每种可能的情况,任何布尔语句对都必须为真。 您为哪个订单提供date对的方法并不重要。

 -(BOOL)isDateInterval:(NSString*)startDate1 and:(NSString*)endDate1 fallsWithin:(NSString*)startDate2 and:(NSString*)endDate2 { BOOL isWithinrange; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"dd/MM/yyyy hh:mmaa"]; NSTimeInterval ti1 = [[formatter dateFromString:startDate2] timeIntervalSince1970]; NSTimeInterval ti2 = [[formatter dateFromString:endDate2] timeIntervalSince1970]; NSTimeInterval ti3 = [[formatter dateFromString:startDate1] timeIntervalSince1970]; NSTimeInterval ti4 = [[formatter dateFromString:endDate1] timeIntervalSince1970]; if( (ti3 > ti1 && ti3 < ti2) || (ti3 < ti1 && ti4 > ti1) ) { //Date with in range isWithinrange=TRUE; } else { isWithinrange=FALSE; //Not with in range } return isWithinrange; } 

我们有四个date, firstStartDatefirstEndDatesecondStartDatesecondEndDate 。 假设两个开始date实际上总是早于相应的结束date,那么可以通过六种方式来订购:

 + start1 start2 end2 end1 + start1 start2 end1 end2 x start1 end1 start2 end2 + start2 start1 end2 end1 + start2 start1 end1 end2 x start2 end2 start1 end1 

其中四个sorting – 我标记的+ – 构成给定时间段的重叠。 重叠有这个共同点:无论哪个开始date在另一个之前,匹配的结束date必须在第二次开始之后。 这是很容易的代码:

 @implementation NSDate (WSSComparisons) - (BOOL)WSSIsLaterThanDate:(NSDate *)date { return (self == [self laterDate:date]); } - (BOOL)WSSIsEarlierThanDate:(NSDate *)date { return (self == [self earlierDate:date]); } @end 

 - (BOOL)periodFromDate:(NSDate *)firstStartDate toDate:(NSDate *)firstEndDate overlapsPeriodFromDate:(NSDate *)secondStartDate toDate:(NSDate *)secondEndDate { /* // Sanity check? if( [firstStartDate WSSIsLaterThanDate:firstEndDate] || [secondStartDate WSSIsLaterThanDate:secondEndDate] ){ return NO; } */ // If firstStartDate is before secondStartDate and firstEndDate isn't, // they overlap if( [firstStartDate WSSIsEarlierThanDate:secondStartDate] ){ return [firstEndDate WSSIsLaterThanDate:secondStartDate]; } else { // Likewise for secondStartDate return [secondEndDate WSSIsLaterThanDate:firstStartDate]; } } 

如果你的“date”碰巧是string,逻辑是相同的; 只需添加一个NSDateFormatter 与适当的格式和parsing。

看看苹果的NSDate 文档 。

看看方法:

isEqualToDate:earlierDate:laterDate:compare:

这些是你需要检查date是否在一个范围内的方法。 您需要先将date文本转换为NSDate。 请参阅NSDateFormatter 类的参考 。

希望这能让你走上正确的道路。