views:

3273

answers:

4

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.

+3  A: 

If you want to know if the current date falls between two given points in time (9AM - 5PM on 7/1/09), use NSCalendar and NSDateComponents to build NSDate instances for the desired times and compare them with the current date.

If you want to know if the current date falls between these two hours every day, then you could probably go the other way. Create an NSDateComponents object with and NSCalendar and your NSDate and compare the hour components.

kperryua
+7  A: 

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;
}
Brock Woolf
That looks good, but you might want to rename it something like this:+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDatein order to avoid confusion.
Jeff Kelley
Good point Jeff, your idea does make it more clear. I will update my answer.
Brock Woolf
Code golf! The compiler should short-circuit the comparison, making the efficiency roughly equal to what you have: return (([date compare:beginDate] != NSOrderedDescending)
Tim
(Note: I addressed this misconception in my answer.)
Quinn Taylor
Can someone help me with an example on how to use this code.
nishantcm
+1  A: 

You can take a look at the answers to this question for more information on how create NSDate objects for a specific time period, such as 5pm-9pm.

Marc Charbonneau
+3  A: 

For the first part, use the answer from @kperryua to construct the NSDate objects you want to compare with. From your answer to your own question, it sounds like you have that figured out.

For actually comparing the dates, I totally agree with @Tim's comment on your answer. It's more concise yet actually exactly equivalent to your code, and I'll explain why.

+ (BOOL) date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate {
    return (([date compare:beginDate] != NSOrderedAscending) && ([date compare:endDate] != NSOrderedDescending));
}

Although it may seem that the return statement must evaluate both operands of the && operator, this is actually not the case. The key is "short-circuit evaluation", which is implemented in a wide variety of programming languages, and certainly in C. Basically, the operators & and && "short circuit" if the first argument is 0 (or NO, nil, etc.), while | and || do the same if the first argument is not 0. If date comes before beginDate, the test returns NO without even needing to compare with endDate. Basically, it does the same thing as your code, but in a single statement on one line, not 5 (or 7, with whitespace).

This is intended as constructive input, since when programmers understand the way their particular programming language evaluates logical expressions, they can construct them more effectively without so much about efficiency. However, there are similar tests that would be less efficient, since not all operators short-circuit. (Indeed, most cannot short-circuit, such as numerical comparison operators.) When in doubt, it's always safe to be explicit in breaking apart your logic, but code can be much more readable when you let the language/compiler handle the little things for you.

Quinn Taylor
Understandable. I had no idea that Objective-C would short-circuit evaluations. I'll take your word on it that it does. Thanks for clearing that up for me. I Suppose I will mark you as correct since your answer is more concise than my own... and I learned something :)
Brock Woolf
No need to take my word for it — I advocate the principle "trust, but verify" (thanks, Ronald Reagan!) if only to satisfy my curiosity and learn the bounds of when something works and doesn't. You can verify it by doing an you'll notice that if the first one returns 0, the second one will never be called.
Quinn Taylor
Sure, I will code up something to verify that.
Brock Woolf