• 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: NSPredicate & dates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSPredicate & dates


  • Subject: Re: NSPredicate & dates
  • From: Chris Blunt <email@hidden>
  • Date: Wed, 24 Aug 2005 13:31:00 +0100

Thanks Mark,

Looking at your code, It appears I need to build a 'predicate builder' sheet like the smart playlists sheet in iTunes...I was for now just using a text field bound to the filterPredicate in the hope I could type something like "date contains "August"" with the intention of building the sheet later... no time like the present I guess!

Cheers,
Chris
--
www.chrisblunt.com

On 24 Aug 2005, at 12:58, Mark Ackerman wrote:

Chris,

Perhaps this might be helpful. Completely untested, but I've been using similar things in an app.

- (NSPredicate *)dateWithinLast30DaysPredicate
{
   //
   // get the current date
   //
   NSDate *now = [NSDate date];

//
// get the date 30 days prior to current date
//
// because CoreData uses NSDate rather than NSCalendarDate, we'll use
// an NSCalendar to do the math;
//
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:-30];
NSDate *date30DaysAgo = [calendar
dateByAddingComponents:dateComps
toDate:now options:0];
[dateComps release];


   //
   // alternatively, we could use:
   //
   // NSCalendarDate *now = [NSCalendarDate calendarDate];
   // NSCalendarDate *date30DaysAgo = [now
   //      dateByAddingYears:0
   //      months:0 days:-30
   //      hours:0 minutes:0
   //      seconds:0];
   //

//
// create the predicate, assuming that the entity with which the predicate
// will be evaluated has a "dateToCompare" attribute
//
NSPredicate *dateInLast30DaysPredicate = [NSPredicate
predicateWithFormat:@"dateToCompare > %@", date30DaysAgo];


   return dateInLast30DaysPredicate;
}

Mark

On Aug 23, 2005, at 9:37 PM, Chris Blunt wrote:

Hi,

Can anyone point me in the direction of how to compare dates in an NSPredicate? My date is stored as an NSCalendarDate* in my model class. I would like the filterPredicate (search field or smart- group) to be able to return records where for example 'date is in the last 30 days', as in iTunes etc. I have got the filter working fine for things such as "amount == 0" or "notes contains 'string'", but dates seem not to be covered by NSPredicate?

Many thanks,
Chris
--
www.chrisblunt.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40earthlink.net


This email sent to email@hidden



_______________________________________________ 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: 
 >NSPredicate & dates (From: Chris Blunt <email@hidden>)
 >Re: NSPredicate & dates (From: Mark Ackerman <email@hidden>)

  • Prev by Date: Finder Color Label
  • Next by Date: Re: Finder Color Label
  • Previous by thread: Re: NSPredicate & dates
  • Next by thread: Slightly OT (Rant: Problems with CIImageAccumulator and friends)
  • Index(es):
    • Date
    • Thread