Re: How do I compare two NSDates using NSPredicate & Core Data
Re: How do I compare two NSDates using NSPredicate & Core Data
- Subject: Re: How do I compare two NSDates using NSPredicate & Core Data
- From: Ashley Clark <email@hidden>
- Date: Fri, 9 Apr 2010 12:52:21 -0500
On Apr 5, 2010, at 7:02 PM, Sean McBride wrote:
> On Sun, 4 Apr 2010 23:15:16 -0400, Michael A. Crawford said:
>
>> Thus far I've gotten away with using -predicateWithFormat and scalar
>> values. I now need to compare a couple of NSDate instances but am not
>> sure how to code it up with NSPredicate. Consider me a 'visual' learner.
>
> I'm pretty sure you can use just <, >, ==, etc. with NSDates in NSPredicates.
One thing that many people overlook is that NSDates *always* include a time component. And if that time wasn't specified explicitly it typically defaults to a preset value set in the editing control (if one was used) or the current time at the time the date string was parsed.
So, if you were expecting to compare a set of NSDate objects to be on a particular date irrespective of their time value... you have a bit of work to do. A simple way to approximate the test is to look at a time range from midnight to midnight-1 second of the following day.
That of course leaves another issue aside though, that of differing timezones. All NSDate objects are converted to the local timezone by default and the original timezone information is not stored in the object, so an NSDate created as {2010/01/01 23:59 EDT} when read back at some later point when the computer is now on the west coast (PDT) would show as {2009/12/31 20:59 PDT}.
All of that to say, comparing dates with predicates (or at all) is hard.
Ashley
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden