2

我想检查日期限制* (例如:- 22/07/2013 2:30PM -22/07/2013 8:30PM) * 是否有另一个时间限制* (例如:- 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)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”,因为一个时间间隔的一部分进入另一个时间间隔

4

4 回答 4

2

This code will give you what what you want. It checks if either the start date or end date of both intervals is between the start date and end date of the other interval.

- (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;
}

To account for each possible case, any of the pairs of boolean statements have to be true. It doesn't matter which order you supply the pairs of dates to the method.

于 2013-07-30T11:37:22.423 回答
1
-(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;
}
于 2013-07-27T18:32:20.357 回答
1

我们有四个日期,firstStartDatefirstEndDatesecondStartDatesecondEndDate。假设这两个开始日期实际上总是早于它们相应的结束日期,则可以通过六种方式对它们进行排序:

+ 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

其中四个排序——我已经标记的那些+——构成了给定时间段的重叠。重叠有一个共同点:无论哪个开始日期早于另一个,匹配的结束日期必须在第二个开始之后。这很容易放入代码中:

@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];
    }
}

如果您的“日期”恰好是字符串,则逻辑是相同的;只需添加NSDateFormatter 适当格式的并解析即可。

于 2013-07-30T18:51:26.033 回答
0

查看 Apple 提供的NSDate 文档

看看方法:

isEqualToDate:, earlierDate:,laterDate:compare:

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

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

于 2013-07-27T15:41:05.730 回答