How to Check if an NSDate occurs between two other NSDates

Brock Woolf picture Brock Woolf · Jul 2, 2009 · Viewed 38.2k times · Source

I am trying to figure out whether or not the current date falls within a date range using NSDate.

For example, you can get the current date/time using NSDate:

NSDate rightNow = [NSDate date];

I would then like to use that date to check if it is in the range of 9AM - 5PM.

Answer

Brock Woolf picture Brock Woolf · Jul 2, 2009

I came up with a solution. If you have a better solution, feel free to leave it and I will mark it as correct.

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;

    if ([date compare:endDate] == NSOrderedDescending) 
        return NO;

    return YES;
}