• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDate in a Range
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >NSDate in a Range (From: malcom <email@hidden>)
 >Re: NSDate in a Range (From: "Shawn Erickson" <email@hidden>)

  • Prev by Date: Re: [SOLVED] createDirectoryAtPath anomaly between system versions?
  • Next by Date: Re: Core Data: Single entity in NSDocument?
  • Previous by thread: Re: NSDate in a Range
  • Next by thread: Re: Re: NSDate in a Range
  • Index(es):
    • Date
    • Thread