我想检查日期限制*(例如:- 22/07/2013 2:30PM -22/07/2013 8:30PM )* 是否有另一个时间限制*< em>(例如:- 22/07/2013 4:30PM -22/07/2013 6:30PM)* 与否。我对这个概念完全陌生。谁能告诉我如何做到这一点。
我们将不胜感激任何帮助
下面是我试过的代码:
- (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)isDateIntervalNSString*)startDate1 andNSString*)endDate1 fallsWithinNSString*)startDate2 andNSString*)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”,因为一个时间间隔的一部分正在进入另一个时间间隔
此代码将为您提供所需的内容。它检查两个间隔的开始日期或结束日期是否在另一个间隔的开始日期和结束日期之间。
- (BOOL)timeIntervalFromDateNSString *)dateString toDateNSString *)anotherDateString overlapsWithTimeIntervalFromDateNSString *)date2String toDateNSString *)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 earlierDatetherStartDate] == startDate;
BOOL endDateIsLaterThanOtherStartDate = [endDate laterDatetherStartDate] == endDate;
BOOL startDateIsEarlierThanOtherEndDate = [startDate earlierDatetherEndDate] == startDate;
BOOL endDateIsLaterThanOtherEndDate = [endDate laterDatetherEndDate] == endDate;
BOOL otherStartDateIsEarlierThanStartDate = [startDate earlierDatetherStartDate] == otherStartDate;
BOOL otherEndDateIsLaterThanStartDate = [otherEndDate laterDate:startDate] == otherEndDate;
BOOL otherEndDateIsEarlierThanStartDate = [startDate earlierDatetherEndDate] == otherEndDate;
BOOL otherEndDateIsLaterThanEndDate = [endDate laterDatetherEndDate] == otherEndDate;
isWithinRange = (startDateIsEarlierThanOtherStartDate && endDateIsLaterThanOtherStartDate) || (startDateIsEarlierThanOtherEndDate && endDateIsLaterThanOtherEndDate) || (otherStartDateIsEarlierThanStartDate && otherEndDateIsLaterThanStartDate) || (otherEndDateIsEarlierThanStartDate && otherEndDateIsLaterThanEndDate);
return isWithinRange;
}
为了说明每种可能的情况,任何一对 bool 语句都必须为真。向方法提供日期对的顺序无关紧要。
关于iphone - 如何检查限制的日期和时间是否与另一个限制重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17899434/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |