iphone - 如何检查限制的日期和时间是否与另一个限制重叠?
<p><p>我想检查日期限制*<em>(例如:- 22/07/2013 2:30PM -22/07/2013 8:30PM )</em>* 是否有另一个时间限制*< em>(例如:- 22/07/2013 4:30PM -22/07/2013 6:30PM)</em>* 与否。我对这个概念完全陌生。谁能告诉我如何做到这一点。</p>
<p>我们将不胜感激任何帮助</p>
<p>下面是我试过的代码:</p>
<pre><code>- (void)viewDidLoad
{
;
;
}
-(BOOL)isDateInterval:(NSString*)startDate1 and:(NSString*)endDate1 fallsWithin:(NSString*)startDate2 and:(NSString*)endDate2
{
BOOL isWithinrange;
NSDateFormatter *formatter = [init];
;
NSTimeInterval ti1 = [ timeIntervalSince1970];
NSTimeInterval ti2 = [ timeIntervalSince1970];
NSTimeInterval ti3 = [ timeIntervalSince1970];
NSTimeInterval ti4 = [ 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;
}
</code></pre>
<p>我希望输出为“isWithin”,因为一个时间间隔的一部分正在进入另一个时间间隔</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>此代码将为您提供所需的内容。它检查两个间隔的开始日期或结束日期是否在另一个间隔的开始日期和结束日期之间。</p>
<pre><code>- (BOOL)timeIntervalFromDate:(NSString *)dateString toDate:(NSString *)anotherDateString overlapsWithTimeIntervalFromDate:(NSString *)date2String toDate:(NSString *)anotherDate2String {
BOOL isWithinRange = NO;
NSDateFormatter *formatter = [ init];
formatter.dateFormat = @"dd/MM/yyyy hh:mmaa";
NSDate *date = ;
NSDate *anotherDate = ;
NSDate *date2 = ;
NSDate *anotherDate2 = ;
NSDate *startDate = ;
NSDate *endDate = ;
NSDate *otherStartDate = ;
NSDate *otherEndDate = ;
BOOL startDateIsEarlierThanOtherStartDate = == startDate;
BOOL endDateIsLaterThanOtherStartDate = == endDate;
BOOL startDateIsEarlierThanOtherEndDate = == startDate;
BOOL endDateIsLaterThanOtherEndDate = == endDate;
BOOL otherStartDateIsEarlierThanStartDate = == otherStartDate;
BOOL otherEndDateIsLaterThanStartDate = == otherEndDate;
BOOL otherEndDateIsEarlierThanStartDate = == otherEndDate;
BOOL otherEndDateIsLaterThanEndDate = == otherEndDate;
isWithinRange = (startDateIsEarlierThanOtherStartDate && endDateIsLaterThanOtherStartDate) || (startDateIsEarlierThanOtherEndDate && endDateIsLaterThanOtherEndDate) || (otherStartDateIsEarlierThanStartDate && otherEndDateIsLaterThanStartDate) || (otherEndDateIsEarlierThanStartDate && otherEndDateIsLaterThanEndDate);
return isWithinRange;
}
</code></pre>
<p>为了说明每种可能的情况,任何一对 bool 语句都必须为真。向方法提供日期对的顺序无关紧要。</p></p>
<p style="font-size: 20px;">关于iphone - 如何检查限制的日期和时间是否与另一个限制重叠?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/17899434/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/17899434/
</a>
</p>
页:
[1]