Re: NSDate in a Range
Re: NSDate in a Range
- Subject: Re: NSDate in a Range
- From: Andy Lee <email@hidden>
- Date: Fri, 7 Jul 2006 10:02:23 -0400
On Jul 6, 2006, at 7:14 PM, Shawn Erickson wrote:
On 7/6/06, malcom <email@hidden> wrote:
How is possible to check if an NSDate is inside a range between
date A
and date B?
One option is to use -[NSDate laterDate:] and -[NSDate earlierDate:].
This won't work if there is any chance your test date will equal
dateA or dateB, because the docs for these methods don't say what
they return if the dates are equal. (Well, strictly speaking you
could make it work by juggling a bunch of contorted logic to test for
the equality cases, but it doesn't make sense to do so when Shawn's
other suggestion works much better.)
Another option is to get the time interval since the same reference
data and do a numerical comparison.
Besides actually working :), this approach makes it easy to include
or exclude the endpoints, by using either >= and <=, or < and >.
On Jul 6, 2006, at 7:20 PM, John Pannell wrote:
Assuming the existence of NSDate *dateA and NSDate *dateB (and
NSDate *myDateOfInterest),
Good point -- you should handle the nil cases.
if(([myDateOfInterest compare:dateA] == NSOrderedDescending) &&
([myDateOfInterest compare:dateB] == NSOrderedAscending)){
NSLog(@"my date is between A and B");
}
This works, but if you want to include endpoints it involves a bit
more typing to check for NSOrderedSame. I also find it harder to
read than converting the dates to milliseconds, but that's me.
--Andy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden