Using Core Data with NSPredicate and NSDate - objective-c

I have a Event table that saves a NSString *action and a NSDate *date. I want to filter the array retrieved from core data to display all the actions performed in the last hour and the next hour not depending on the day it was saved. Example on Tuesday a action was saved on 16:00 but Today is Friday 17:00 and I still want to retrieve that action saved on Tuesday and any action within that time frame.

For anyone stuck with this problem. Changed my Event Table to hold a integer (hour of the action) and retrieved it like this
NSDate *currentFullDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:currentFullDate];
NSInteger currentHour = [components hour];
NSInteger hourAgo = currentHour -1;
NSInteger nextHour = currentHour +1;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Event"];
request.predicate = [NSPredicate predicateWithFormat:#"(hour >=%#) AND (hour <= %#)", hourAgo, nextHour];
request.fetchLimit = 5;
NSError *error;
NSArray *eventArray = [managedObjectContext executeFetchRequest:request error:&error];
for (Event *event in eventArray)
{
}

Related

How to get all the date of last one month

i want to get all the dates from yesterday date to one month..
like today is 19 may, so i need all the date from 18 may to 18 April.
please help.
You can use this code.It works.
NSDate *currentDate = [NSDate date];
NSLog(#"Current Date = %#", currentDate);
NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.month = -1;
NSDate *currentDatePlus1Month = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSLog(#"Date = %#", currentDatePlus1Month );
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *days = [[NSDateComponents alloc] init];
NSMutableArray* arr =[[NSMutableArray alloc]init];
NSInteger dayCount = 0;
while ( TRUE ) {
[days setDay: ++dayCount];
NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: currentDatePlus1Month options: 0];
if ( [date compare: currentDate] == NSOrderedAscending ){
[arr addObject:date];
}
if([[arr lastObject] isEqual:[currentDate dateByAddingTimeInterval:-60*60*24*1]])
{
NSLog(#"%lu",(unsigned long)arr.count);
break;
}
// Do something with date like add it to an array, etc.
}
if you find all dates you can remove count and get all dates in array.
To achieve this, I think you should have an Array holding all those dates. I'll write pseudocode about the logic here.
INIT dateArray
NSDate pastDate = (today).yesterday
NSDate lastMonth = pastDate.lastMonth()
WHILE pastDate > lastMonth // pastDate is after lastMonth
dateArray.add(pastDate)
pastDate = pastDate.yesterday
END WHILE
About how to turn this pseudocode into real code is another story (this would be quite long). Hope this help.
PS: If you'd like Objective-C solution, please comment. I'll take my time write it for you ;)

Parse this month only

I am new to parse and I need help retrieve objects that are in the current month.
First I have a class called food and have a column of type Date called food_date
I am assuming I have to use greaterThan and less Than, but I am unsure how to.
NSDate *thisMonth = [NSDate date];
PFQuery *foodList = [PFQuery queryWithClassName:#"food"];
[foodList whereKey:#"food_date"
I think you need something like this :
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
components.day = 1;
// Get the date of the first day of the current month
NSDate *minimumDate = [calendar dateFromComponents:components];
components.month = components.month + 1;
// Get the date of the first day of the next month
NSDate *maximumDate = [calendar dateFromComponents:components];
PFQuery *foodList = [PFQuery queryWithClassName:#"food"];
[foodList whereKey:#"food_date" greaterThanOrEqualTo:minimumDate];
[foodList whereKey:#"food_date" lessThan:maximumDate];
[foodList findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d foods.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(#"%#", object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
I hope that help.

Create an Array of NSDates like in Ruby

I want to create an array of NSDates starting from today to next month. This can easily be done in Ruby using Time.now..(Time.now + 30.days)
How can I create an array of dates just like in Ruby in Objective C?
Any ObjC solution is unfortunately going to be far more verbose than that Ruby code.
The correct way to make the calculation is with NSDateComponents:
NSMutableArray * dateArray = [NSMutableArray array];
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * plusDays = [NSDateComponents new];
NSDate * now = [NSDate date];
for( NSUInteger day = 0; day < NUMDAYS; day++ ){
[plusDays setDay:day];
[dateArray addObject:[cal dateByAddingComponents:plusDays toDate:now options:0]];
}
To make the procedure more convenient (if you need to do it more than a few times), you could put this loop into a category method on NSCalendar, with NUMDAYS replaced with the argument and substituting self for cal.
There's nothing built in to do this quite as concisely as the Ruby you've posted. Breaking the problem down, you need a way to get the day after a particular date. Here's a function that will do that:
NSDate *CalendarDayAfterDate(NSDate *date)
{
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:components toDate:date options:0];
}
Next, you need to get an array of days one after the other:
NSDate *today = [NSDate date];
NSMutableArray *dates = [NSMutableArray arrayWithObject:today];
for (NSUInteger i=0; i<30; i++) {
NSDate *tomorrow = CalendarDayAfterDate(today);
[dates addObject:tomorrow];
today = tomorrow;
}
After much downvoting and commenting, here's my REVISED answer...
-(NSDate *)nextDayFromDate:(NSDate *)originalDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponent = [NSDateComponents new];
dateComponent.day = 1;
NSDate *tomorrow = [calendar dateByAddingComponents:dateComponent toDate:originalDate options:0];
return tomorrow;
}
NSMutableArray *dateArray = [NSMutableArray array];
NSDate *now = [NSDate date];
[dateArray addObject:now];
for (int i=0;i<31;i++) {
NSDate *firstDate = [dateArray objectAtIndex:i];
NSDate *newDate = [self nextDayFromDate:firstDate];
[dateArray addObject:newDate];
}
What this does is use the NSCalendar API to add a "day interval" to any given NSDate. Add "Now" to the array, then do a loop 30 times, each time using the previous NSDate object as input to the logic.

Core Data Predicate Date Comparison

Im trying to fetch all the objects in an entity matching a user selectedDate (it's an NSDate).
The Core Data code is fine but my predicate keeps returning 0 results, the dates are the same in the database as the user is selecting.
How should the selectedDate be compared with a date from an entity using a predicate?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(eDate = %#)", selectedDate];
Your predicate looks to be fine.
The reason you're finding zero result are returned however may be that the dates aren't entirely the same.
For example:
05/04/2012 13:37:00 will not match with 05/04/2012 13:37:01 as these two values are not exactly the same.
Do you want to check the date (day, month, year) as well as the time?
If you only want to check the date, you should create a start date and end date and compare them using the user selected date as a frame of reference.
Something similar to this should create a date and time for 00:00:00.
//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
//set date components
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
//return date relative from date
return [calendar dateFromComponents:dateComponents];
Create another date by setting the hours, minutes and seconds to 23:59:59 and check that your user selected date falls between these ranges.
[NSPredicate predicateWithFormat:#"date BETWEEN %#", [NSArray arrayWithObjects:startOfDay, endOfDay, nil]]
While in human communication date often is equal with day, it is not the same with NSDate objects: A NSDate represents a single moment in time. Dates that are different just in seconds aren't equal. And actually they dont represent days, month, year at all, as this is different from calendar system to calendar system
you must define for yourself if dates in the same minute, hour, day … are equal. So basically you must teach the program to allow some fuzziness.
here for minute resolution
NSDate *startDate = ....;
NSTimeInterval length;
[[NSCalendar currentCalendar] rangeOfUnit:NSMinuteCalendarUnit
startDate:&startDate
interval:&length
forDate:startDate];
NSDate *endDate = [startDate dateByAddingTimeInterval:length];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(eDate >= %#) AND (eDate < %#)", startDate, endDate];
For dates on the same day (aware of Timezones and Daylight Saving Times) you just would change this:
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
startDate:&startDate
interval:&length
forDate:startDate];
I can get working with some modifications to the accepted answer and using the iOS 8 API for create dates with time offset. The code:
NSCalendar *calendar = [[FFSharedCalendar singleton] getGregorianCalendar];
NSDate *startOfDay = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:0];
NSDate *endOfDay = [calendar dateBySettingHour:23 minute:59 second:59 ofDate:[NSDate date] options:0];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"createdAt > %# AND createdAt < %#", startOfDay, endOfDay];
NSArray* plans = [Plan MR_findAllWithPredicate:predicate];
Hope it helps someone
I've recently spent some time attempting to solve this same problem and have resolved the following, now updated for iOS 8 and above...
NSDate *dateDay = nil;
NSDate *dateDayStart = nil;
NSDate *dateDayNext = nil;
dateDay = <<USER_INPUT>>; //for example - selectedDate
dateDayStart = [[NSCalendar currentCalendar] startOfDayForDate:dateDay];
// dateDayNext EITHER
dateDayNext = [dateDayStart dateByAddingTimeInterval:(24 * 60 * 60)];
// dateDayNext OR
NSDateComponents *dateComponentDay = nil;
dateComponentDay = [[NSDateComponents alloc] init];
[dateComponentDay setDay:1];
dateDayNext = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponentDay
toDate:dateDayStart
options:NSCalendarMatchNextTime];
...and the NSPredicate for the Core Data NSFetchRequest (as already shown above in other answers)...
[NSPredicate predicateWithFormat:#"(dateAttribute >= %#) AND (dateAttribute < %#)", dateDayStart, dateDayNext]]
I query in between 1 second above and below my actual Date. In my case its ok to add and subtract 1 second as I query for the restaurant orders so I know that it takes some time to punch a new order, for safe side I have 1 more orderNo field too.
let fetchRequest: NSFetchRequest = Order.fetchRequest()
let date_formatter = DateFormatter()
date_formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
date_formatter.timeZone = TimeZone(abbreviation: "UTC")
let createdDate = date_formatter.date(from: clientCreatedStr)
let calendar = NSCalendar.current
//as matching exact same datetime doesnt return anything
let onesecondafter = calendar.date(byAdding: .second, value: 1, to: createdDate!)
let onesecondbefore = calendar.date(byAdding: .second, value: -1, to: createdDate!)
let predicate = NSPredicate(format: "offlineUniqueId == %# AND (clientCreatedDate >= %# AND clientCreatedDate <= %#)", offlineUniqueId, onesecondbefore! as NSDate, onesecondafter! as NSDate)
fetchRequest.predicate = predicate

NSNumber from NSDate

I'm attempting to get around a date validation that refuses to take anything earlier than tomorrow.
So far I have this:
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
// Negative one day, in seconds (-60*60*24)
NSLog(#"%#", [NSDate date]);
// "yyyy-MM-dd HH:mm:ss Z", accurate assuming Z = +0000
NSLog(#"%#", dateY);
// "yyyy-MM-dd HH:mm:ss Z", same accuracy (minus one day)
That's great, but dateY is not an NSNumber. I need an NSNumber for the comparison, but I can't find anything that works. (I don't even know how an NSNumber can be 2011-04-14 13:22:29 +0000, anyway...)
I can use NSDateFormatter to convert an NSDate into an NSString, so if it would be possible to take that string and convert it to the required NSNumber (as opposed to directly converting the NSDate to an NSNumber, which I can't seem to find help with either), that would be fine.
- (BOOL)validateDueDate:(id *)ioValue error:(NSError **)outError {
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSNumber *tis1970 = [NSNumber numberWithDouble:[dateY timeIntervalSince1970]];
NSLog(#"NSNumber From Date : %#", tis1970);
NSLog(#"Date From NSNumber : %#", [NSDate dateWithTimeIntervalSince1970:[tis1970 doubleValue]]);
// Due dates in the past are not valid
// Enforced that a due date has to be >= today's date
if ([*ioValue compare:[NSDate date]] == NSOrderedAscending) {
if (outError != NULL) {
NSString *errorStr = [[[NSString alloc] initWithString:#"Due date must be today or later."] autorelease];
NSDictionary *userInfoDictionary = [NSDictionary dictionaryWithObject:errorStr forKey:#"ErrorString"];
NSError *error = [[[NSError alloc]
initWithDomain:TASKS_ERROR_DOMAIN
code:DUEDATE_VALIDATION_ERROR_CODE
userInfo:userInfoDictionary] autorelease];
*outError = error;
}
return NO;
} else {
return YES;
}
}
Right now, the user is not allowed to choose a date before tomorrow. errorStr lies. Before today makes more sense than before tomorrow as a rule for refusing to save the date, so I've been fighting with this thing to let me use yesterday in place of today, rather than looking any deeper.
Edit: Using NSOrderedSame allows any date to be selected without an error. That won't do.
You can convert an NSDate to an NSNumber like this:
NSDate *aDate = [NSDate date];
NSNumber *secondsSinceRefDate = [NSNumber numberWithDouble:[aDate timeIntervalSinceReferenceDate]];
and convert back like:
aDate = [NSDate dateWithTimeIntervalSinceReferenceDate:[NSNumber doubleValue]];
All that is needed to get a NSNumber is
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSNumber *tis1970 = [NSNumber numberWithDouble:[dateY timeIntervalSince1970]];
NSLog(#"NSNumber From Date : %#", tis1970);
NSLog(#"Date From NSNumber : %#", [NSDate dateWithTimeIntervalSince1970:[tis1970 doubleValue]]);
You should never use 86400 to calculate date differences, because not all days have 86,400 seconds in them. Use NSDateComponents instead:
- (BOOL)validateDueDate:(NSDate *)dueDate error:(NSError *)error {
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
//adjust the components to tomorrow at the first instant of the day
[components setDay:[components day] + 1];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *tomorrow = [calendar dateFromComponents:components];
NSDate *earlierDate = [dueDate earlierDate:tomorrow];
if ([earlierDate isEqualToDate:dueDate]) {
//the dueDate is before tomorrow
if (error != nil) {
NSString *errorStr = [[[NSString alloc] initWithString:#"Due date must be today or later."] autorelease];
NSDictionary *userInfoDictionary = [NSDictionary dictionaryWithObject:errorStr forKey:NSLocalizedDescriptionKey];
*error = [[[NSError alloc] initWithDomain:TASKS_ERROR_DOMAIN code:DUEDATE_VALIDATION_ERROR_CODE userInfo:userInfoDictionary] autorelease];
}
return NO;
}
return YES;
}
WARNING: code typed in a browser. Caveat Implementor