问题
I want to check whether the date limit*(Ex:- 22/07/2013 2:30PM -22/07/2013 8:30PM )* comes with in another time limit*(Ex:- 22/07/2013 4:30PM -22/07/2013 6:30PM )* or not. I am completely new to this concept. Can anyone please tell me how to do this.
Any help will be highly appreciated
Below is the code I tried:
- (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;
}
I want the output to be "isWithin" as because part of the time interval of one is coming into the other time interval
回答1:
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.
回答2:
-(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;
}
回答3:
We've got four dates, firstStartDate
, firstEndDate
, secondStartDate
, and secondEndDate
. Assuming that the two start dates are always actually earlier than their corresponding end dates, there are six ways these can be ordered:
+ 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
Four of those orderings -- the ones I've marked with +
-- constitute overlaps of the given time periods. The overlaps have this in common: whichever start date comes before the other, the matching end date must be after that second start. This is pretty easy to put into code:
@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];
}
}
If your "dates" happen to be strings, the logic is the same; just add an NSDateFormatter
with the appropriate format and parse.
回答4:
Take a look at the NSDate
documentation from Apple.
Look at the methods:
isEqualToDate:
, earlierDate:
, laterDate:
, and compare:
These are the methods you'll need to check if the dates are within a range. You'll need to convert your date text to an NSDate first. See the NSDateFormatter
class reference.
Hopefully this gets you on the right path.
来源:https://stackoverflow.com/questions/17899434/how-to-check-whether-the-date-time-of-a-limit-overlaps-another-limit-or-not