Re: NSPredicate & dates
Re: NSPredicate & dates
- Subject: Re: NSPredicate & dates
- From: Mark Ackerman <email@hidden>
- Date: Wed, 24 Aug 2005 07:58:05 -0400
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