NSPredicate with IN for NSDate - objective-c

I wanted to fetch the objects from entity whose startDate or endDate is greater than the dates
provided in an array.
I am currently using IN for predicate as follows,
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K IN %# OR %K in %#", kEntityEventAttributeStartDate, inArray, kEntityEventAttributeEndDate, inArray];
request setPredicate:predicate];
But I think it checks for equal dates Where as I want dates which are greater.
Any Idea how to achieve this with NSPredicate.
Thanks in advance.

You can first sort the date array like:
NSArray *dateArraySorted = [inArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"Date" ascending:YES]]];
//Select the smallest date - if the date is greater than this then there is no point in checking against others and vice-versa.
NSDate *smallestDate = dateArraySorted[0];
From here, I guess you know how to create the predicate.

I solved it by creating a predicate which has a start date and end date, I query the model to get all the dates which are between the provided range.
Thanks for the help and suggestions.

Related

Fetch objects from Coredata using NSDate

I have 10 records in database. I want to fetch the record where date == mydate.
For eg, there are 2 records in database, which has date of 502479617.512 (in milliseconds).
I am converting the above date to NSDate using
[NSDate dateWithTimeIntervalSinceReferenceDate:502479617.512].
I am getting 2016-12-03 17:40:17 +0000
I have date parameter '502479617.500' which returns same NSDate 2016-12-03 17:40:17 +0000.
But when i try to fetch from coredata, i am getting 0 objects. It is because of milliseconds in the database. How can i discard milliseconds to fetch the records which has same date, time and seconds.
NSDate properties will be stored as NSDate attributes, unless the "Use scalar properties for primitive data types" checkbox is marked.
In that case, the core data attribute will be stored as a NSTimeInterval (double).
You can do it in either of two ways:
First, you could eliminate the seconds decimals when storing the NSDate. It could be automated by overriding didSet for the property. You could also simply do it wherever you do the conversion you describe.
Alternatively, you could specify an interval of one second when fetching. Your fetch request would then need a predicate of this kind:
NSPredicate(format: "date >= %# && date < %#", aDate, aDate.addingTimeInterval(60))
Objective-C
[NSPredicate predicateWithFormat:#"date >= %# && date < %#",
aDate, [aDate dateByAddingTimeInterval:60]];
Just save it as NSString. Then convert back to double when you need it.

Backendless filter results based on current date

I have this query
https://api.backendless.com/v1/data/abc?pageSize=100&loadRelations=cinema&sortBy=showing%20asc&where=objectId%3D%272C225111-18FC-DD4F-FF40-E227F49F5B00%27%20AND%20showing%20%3E%20%2714-June-2016%27
Thi query rightly fetches all the results after the date 14 June 2016. But I am giving a hard coded date here, Is there any way I can simply add current time/date here so that It brings all the results prior to current time? I am looking for something like
https://api.backendless.com/v1/data/abc?pageSize=100&loadRelations=cinema&sortBy=showing%20asc&where=objectId%3D%272C225111-18FC-DD4F-FF40-E227F49F5B00%27%20AND%20showing%20%3E%20%CURRENT TIME%27
I have searched alot, and even the documentation refers to hardcode dates or millisecond format only. Any help is appreciated
The easiest option is just to use the UNIX timestamp version of the date, so the string to use in your where clause would be like:
NSString *where = [NSString stringWithFormat:#"showing < %#", [[NSDate date] timeIntervalSince1970]];

NSDate Comparison

I have a list of birthdays. From the list of b'days, I need to select all the b'days which occurs in the next 30 days.
For example : Today's date is 07/13, then I need to list all the b'days which occurs between 07/13 and 08/13.
Is there any built in method to select the dates in this manner.
Thanks in advance
You can use NSPredicate for this:
NSDate *now = [NSDate date];
NSDate *later = [now dateByAddingTimeInterval:(30*24*60*60)];
NSPredicate *pre = [NSPredicate predicateWithFormat:#"SELF >= %# AND SELF <= %#", now, later];
NSArray *upcomingBirthdays = [birthdays filteredArrayUsingPredicate:pre];
You going to have to work the logic out for yourself, here is a link to "Date and Time Programming Guide". It contains some sample code to check if "Date" falls within a Week. Might be a good place to start.
Apple's guide

How can I get the localized date units?

I need a array with date units, for example Year,Month,Day....
I want them to be localized, for example in german it would be Jahr, Monat, Tag ...
It seems I can only get the localized string from date but not the units.
Is there a way to get those units from current calendar directly instead of translating them by hand?
As far as I know, you will need to provide your own translations for those units. You can get the names of the days and months with NSDateFormatter but not the units themselves. How the unit is translated depends on grammatical case and number, so the translation is context sensitive. Don't forget, some languages have grammatical numbers other than just singular and plural!
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CDT"]];
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
[timeZoneComps setHour:16];
//specify whatever day, month, and year is appropriate
NSDate *date=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *monthName = [[df monthSymbols] objectAtIndex:(mymonth-1)];
[df setDateFormat:#"EEE"];
NSString *weekName = [ df stringFromDate:date];
NSLog(#"The Week Name :: %#",weekName);
[df release];
You can use above code and particularly timeZoneWithAbbreviation:#"CDT" represents the abbreviation of various timezones like CDT, GMT,UTC,CST. I think it will be helpful to you.

Current date, convert to multiple timezones, then each needs to check if it is close to another date

I have a project that I need to get the current date/time. From that date/time I need to convert to 6 different timezone's date/time. That is no problem converting the times.
Then from each of the converted date/time I need to see if that date is between two other dates that change every week. I am guessing the times that change everyweek could be an array of dates. The current date/time in that timezone needs to find out which date/time in the array it needs to check itself against. The nearest before and the nearest after. This sounds very confusing just typing this.
Any help pointing me in the right direction would be extremely helpful.
Thankyou
NSDate *date1;
NSDate *date2;
NSDate *date3;
NSTimeInterval firstTimeInterval = [date1 timeIntervalSince1970];
NSTimeInterval secondTimeInterval = [date2 timeIntervalSince1970];
NSTimeInterval thirdTimeInterval = [date3 timeIntervalSince1970];
if (firstTimeInterval<secondTimeInterval && secondTimeInterval<thirdTimeInterval) {
// date2 > date1 and date2 < date3
}
Of course, in my case it would crash since dates have no addresses, but it's just an example... Yours would need to have actual dates in them.
To check if a date is in a specified range just get the unix timestamps and compare them directly like so:
NSDate *lowerLimit = ...
NSDate *upperLimit = ...
NSDate *myDate = ...
BOOL inRange = (lowerLimit.timeIntervalSince1970 <= myDate.timeIntervalSince1970) &&
(upperLimit.timeIntervalSince1970 >= myDate.timeIntervalSince1970);
You could also use NSDate's -compare: method, but I think it's more natural to compare the timestamps.