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
Related
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.
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.
In my application i get from a server some dates.
Some of the dates are in this format : "MMM dd, yyyy hh:mm:ss a" and some others in this : "yyyy-MM-dd HH:mm:ss Z" . I get the dates as strings from the server in one of the above formats. However i want all my dates to be in the first format. So how can i compare if the date is on the second format so i can change it? I know how to change formats , i just dont know how to compare a date with a specific format.
Thanks!
If you know the format that any given returned string is in then you could explicity set the format for each iteration of getting a date and then convert it to whatever format you need. Something like this example;
NSString *myString1 = #"01-30-2012";
NSString *myString2 = #"30-01-2012";
// Now convert the string to a date object
NSDateFormatter *myDateFormat = [[NSDateFormatter alloc] init];
[myDateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date1 = [myDateFormat dateFromString:myString1];
[myDateFormat setDateFormat:#"MM-dd-yyyy"];
NSDate *date2 = [myDateFormat dateFromString:myString2];
[myDateFormat release];
If those are the only formats, just check something simple like if the fifth character is a dash then it's second, otherwise first.
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.
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.