The rangeOfUnit:...
method of NSCalendar
is a convenient method to
compute the start of the current day and the start of tomorrow
in your local time zone:
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *todaysDate;
NSDate *tomorrowsDate;
NSTimeInterval interval;
[cal rangeOfUnit:NSDayCalendarUnit startDate:&todaysDate interval:&interval forDate:now];
tomorrowsDate = [todaysDate dateByAddingTimeInterval:interval];
so that you can use it in the predicate with >=
and <
:
[NSPredicate predicateWithFormat:@"(date >= %@) AND (date < %@)", todaysDate, tomorrowsDate]
to fetch all objects of the current day.
Remark: Don't let the NSLog()
output of NSDate
objects confuse you.
NSDate
represents an absolute point in time and knows nothing about time zones.
NSLog(@"%@", todaysDate)
prints the date according to the GMT time zone and not in your local time zone.
To print the dates according to your time zone, use p todaysDate
in the debugger console (instead of po
),
or print
[todaysDate descriptionWithLocale:[NSLocale currentLocale]]