How do I compare two NSDates using NSPredicate & Core Data

  • 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.

    -Michael
  • 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.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • 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
  • On Fri, 9 Apr 2010 12:52:21 -0500, Ashley Clark 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.

    I get what you're saying, but a date represents an exact moment in time,
    irrespective of location or time zone or any of that.  Sometimes people
    may use an NSDate when they probably should be using NSDateComponents,
    which you can easily do in a Core Data context using a 'transformable'
    attribute.  Comparing NSDateComponents is harder because you also need a
    calendar, etc.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
previous month april 2010 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today