I’m trying to filter an array of objects selecting those where a property of type NSDate are equal to a specific date. However, I only want to compare the date and ignore any time difference.
So I’m using an NSPredicate as so:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"PropertyWithDate == %@",
[NSDate date]];
As both the object is storing the date with time and the value for now I am passing holds both. How do I arrange it to ignore the time part in both during the comparison?
Remember, an NSDate expresses a point in time, independent of timezones, so it only makes sense to talk about the date part of an NSDate with respect to a specific calendar and timezone.
The short answer is that you’ll need to construct two NSDates: one for 12am the day of your date, and one for 12am the day after, and then set up your predicate to look for dates between the two:
To construct
firstDateandsecondDate, you’ll want to useNSCalendar‘scomponents:fromDate:anddateFromComponents:methods. If you want to use the device’s current default calendar and timezone, you can just use[NSCalendar calendar]to get a calendar to use.