Anand Gautam
Anand Gautam

Reputation: 2579

Check if the given date time is between two others date time in Objective C

I am trying to figure out the way to check my date time is between two others date time, for example I am getting current time from my server as "Mar 16 2015 14:17" so I want to check that whether this date time is between two others date time range as "Feb 4 2015 08:00-09:00" or not.

Upvotes: 0

Views: 1348

Answers (3)

Kirit Modi
Kirit Modi

Reputation: 23407

Current date :

NSDate *currentDate = [NSDate date];

NSLog(@"-- %@",currentDate);

Start date :

// Start Date - 2015-03-12 00:00:58 +0000

NSString *stringStartDate = @"2015-03-12 00:00:58";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *dateStart = [formatter dateFromString:stringStartDate];

End date :

// End Date - 2015-03-20 00:00:58 +0000

NSString *stringEndDate = @"2015-03-20 00:00:58";
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *dateEnd = [formatter1 dateFromString:stringEndDate];

Function to check date between two date or not

BOOL isDateCheck = [self isDate:currentDate inRangeFirstDate:dateStart lastDate:dateEnd];

Function

- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate {
    return [date compare:firstDate] == NSOrderedDescending &&
    [date compare:lastDate]  == NSOrderedAscending;
}

Upvotes: 1

Azat
Azat

Reputation: 6795

Try that code if you have three NSDate instances:

NSDate *beginDate, *endDate, *checkDate;
if ([beginDate laterDate:checkDate] == checkDate &&
    [checkDate laterDate:endDate] == endDate) {
    //checkDate in between beginDate and endDate
}

For date parsing use the following:

NSString *inputRange = @"Feb 4 2015 08:00-09:00";
NSRange firstRange = NSMakeRange(0, inputRange.length - 6);
NSString *firstDate = [inputRange substringWithRange:firstRange];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"MMM dd yyyy HH:mm";
NSDate *beginDate = [dateFormatter dateFromString:firstDate];
NSString *secondDate = [inputRange substringWithRange:NSMakeRange(0, inputRange.length - 11)];
secondDate = [secondDate stringByAppendingString:[inputRange substringWithRange:NSMakeRange(inputRange.length - 5, 5)]];
NSDate *endDate = [dateFormatter dateFromString:secondDate];

Upvotes: -1

M_Waly
M_Waly

Reputation: 241

I would suggest the following class method

+ (BOOL)checkDate:(NSDate*)dateToCheck startingDate:(NSDate*)startingDate endDate:(NSDate*)endDate
{
    BOOL returnValue;

    if ([startingDate compare:endDate]==NSOrderedDescending)
    {
        returnValue = [dateToCheck compare:endDate]==NSOrderedAscending && [dateToCheck compare:startingDate]==NSOrderedDescending;

    }
    else if ([startingDate compare:endDate]==NSOrderedAscending)
    {
        returnValue = [dateToCheck compare:startingDate]==NSOrderedAscending && [dateToCheck compare:endDate]==NSOrderedDescending;
    }
    else
    {
    returnValue = [dateToCheck compare:startingDate]==NSOrderedSame && [dateToCheck compare:endDate]==NSOrderedSame;
    }
    return returnValue;
}

Upvotes: 1

Related Questions