Questions about categories - nsdate

I want to do is produce a simple NSDate date in addition to would like
to add methods to add and subtract.
Date * myDate = [NSDate date]; // today
NSLog ("10 days after:%#", [myDate addToDays: 10]);
================= category ==================
#implementation NSDate (AddDate)
- (NSDate *)addToDays:addToDays{
NSDate *returnDate = [***HowGetmyDate*** dateByAddingTimeInterval:60*60*24*addToDays];
return returnDate;
}
how get (myDate)?
The base date need not necessarily today.

Just use self to get your date object.
Category is the extension of the original class, you can operate/implement it as the original one.
#implementation NSDate (AddDate)
- (NSDate *)addToDays:addToDays{
NSDate *returnDate = [self dateByAddingTimeInterval:60*60*24*addToDays];
return returnDate;
}

Related

What format is a Date in CoreData - Date or String?

I have a routine that compares when a record was last updated on a website with the same record saved in CoreData on an iPad. Here, obsLastUpdated is a field in CoreData set as type Date.
Reading the data from CoreData into an NSDate:
NSDate *iPadDate = [[self.recordDetails objectAtIndex:0] obsLastUpdated]; //gets the date from CoreData
I get this warning:
Incompatible pointer types initializing 'NSDate *' with an expression
of type 'NSString *'
It is just a warning however and everything still works. If I try and compare this NSDate iPadDate with another NSDate there are no errors and everything works. But I would like to get to the bottom of the warning.
If I read from CoreData as a string and convert to an NSDate I can't seem to get the formatting correct and the app crashes. I have tried:
NSString *iPadDateStr = [[self.recordDetails objectAtIndex:0] obsLastUpdated];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *iPadDate = [dateFormat dateFromString: iPadDateStr];
I realise that CoreData does not store the date as an NSDate but how exactly is it stored and how do I get around the warning?
CoreData Entity:
UPDATE:
The error was in the class where obsLastUpdated was declared as an NSString. Changed to NSDate and all is well.
Simply define it as NSDate, not NSString:
MyEntity *entity = [self.recordDetails first];
NSDate *iPadDate = entity.obsLastUpdated;

NSDateFormatter ISO8601 NSString do NSdate conversion

i have a problem to convert string date (maybe ISO 8601) to NSDate..
NSString *testDate = #"2016-01-27T18:28:53.344+01:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssZZZZZ"];
NSLog(#"_date_: %#",[formatter dateFromString:testDate]);
NSLog(#"_now_date: %#",[formatter stringFromDate:[NSDate date]]);
NSLog return:
_date_:(null)
_now_date: 2016-02-03T22:36:46.046+01:00
What is the right time format ? if log current date the format maybe appear correct but i can't get NSDate from string
You have fractional second in your date string. Fractional seconds are designated capital S. Try this:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"]
Note that the NSDate you get back is 2016-01-27 17:28:53.344 +0000. This is due to NSDate always represent time in UTC. 17:38 UTC is the same moment in time as 18:38 +01:00.

NSPredicate without consider time component

From my table I want select all rows of a particular date without considering the time component of NSDate.Please any one suggest a NSPredicate to achieve this ?
NSDate describes an absolute point in time and does not have "day" or "time" components.
The same is true for a Core Data "Date" attribute, which is stored as the number of seconds
since 1 January 2001, GMT in the database.
What you probably want is to fetch all objects where some "Date" attribute falls into the
interval given by the start of a given day and the start of the next day.
Therefore you have to compute these dates first, using NSCalendar methods:
NSDate *yourDate = ...;
NSDate *startOfDay, *startOfNextDay;
NSTimeInterval lengthOfDay;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit startDate:&startOfDay interval:&lengthOfDay forDate:yourDate];
startOfNextDay = [startOfDay dateByAddingTimeInterval:lengthOfDay];
Then use a predicate that fetches all objects where the attribute falls between
these two values:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"date >= %# AND date < %#",
startOfDay, startOfNextDay];

CoreData predicate by date

I have a coreData model already setup and data added to it. I want to search for all items from the last 30 days, and then add together a total number of units.
Here's what I got :-
- (void) calculateThirtyDayValues {
NSDate *endDate = [NSDate date];
NSTimeInterval timeSinceRefDate = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval lastThirtyDays = timeSinceRefDate- 2592000;
NSDate *startDate = [NSDate dateWithTimeIntervalSinceReferenceDate:lastThirtyDays];
NSPredicate *predicate = [NSPredicate predicateWithFormat: #"(date >= %#) AND (date <= %#)", startDate, endDate];
}
Basically I have create an NSDate object set todays date, and then created another NSDate object set to 30 days before today. Then trying to predicate all objects from the start date until the present date.
I am getting results returned, but they don't seem to be really matching up with what the totals should be for the past 30 days. It appears to just be returning everything!
Any ideas?
Your predicate seems to be OK, but your date code is incorrect, for a couple of reasons.
[NSDate date] returns the current date, with sub-millisecond precision. So if you create the predicate at 4:15:37 PM local time, this predicate would not find any objects with a date of 5:37:42 PM local time. If you want down-to-the-second precision like that, then you're probably OK. But if you want granularity to a different calendar unit (such as by day), then you need to do more work.
Not every day has 86,400 seconds in it. Thus your attempt to subtract (30*86400) is subtly wrong. You should be letting the calendar object do the math for you:
NSDate *endDate = ...;
NSDateComponents *diff = [[NSDateComponents alloc] init];
[diff setDay:-30];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *startDate = [calendar dateByAddingComponents:diff toDate:endDate options:0];

NSDate value not accessible across methods

Am sorry to ask such a trivial question. Am a newbie to Objective-C, & simply cannot see how to get this working, after having tried several possible ways & google'd around for it. Please help!
My question is simple. I have a class-level NSDate object, which is declared outside any method in the class as:
NSDate *fromDate;
Now, within a method, am setting this value to the date from a DatePicker as:
fromDate = [datePicker date];
Soon after the above assignment, I print its value into the log & it works fine.
NSLog(#"From Date: %#", fromDate);
Now, when I use NSDate's value in another/different method, the value's gone! Why is it not persisted across methods in the same class itself? What can I do for the value to be accessible across methods?
Thanks for your reply.
Hi Remy,
I didn't know Objective-C didn't have class-level variables! Thanks for pointing it out!
Yes, I've set the project (in Xcode) to do ARC (so, I believe that should take care).
Here is the code:
In ViewController.h
....
....
#property (nonatomic, retain) NSDate *historyFromDate;
#property (nonatomic, retain) NSDate *historyToDate;
....
....
-(IBAction) fromDateChosen: (id)sender;
-(void) fetchTheHistory;
In ViewController.m
...
...
#synthesize historyFromDate;
#synthesize historyToDate;
....
....
-(IBAction) fromDateChosen: (id)sender {
NSString *buttonTitle = #"I've chosen the 'FROM' date";
if ([[buttonDateChosen currentTitle] isEqualToString:buttonTitle]) {
NSLog(#"User has chosen the 'From' date");
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
// Get the chosen date value
NSDate *fromDate = [datePicker date];
historyFromDate = fromDate;
// Set the 'to' date label to reflect the user's choice
labelFromDate.text = [dateFormatter stringFromDate:historyFromDate];
NSLog(#"'From' Date Chosen:%#", historyFromDate);
//[dateFormatter stringFromDate:[datePicker date]]);
[self fetchTheMoodHistory];
}
}
...
...
...
-(void) fetchTheHistory {
NSLog(#"Calling fetchTheHistory for the period from %#", historyFromDate);
...
...
}
...
...
fromDateChosen gets called after the user chooses a date form a Date Picker object in the UI.
Within the method 'fromDateChosen', when I print the historyFromDate, the value is correct.
But, when I print it in fetchTheHistory method, the value shows the current date/time (not the one the user chose).
The date property of UIDatePicker is retained by that class, and will be accessible as long as the date picker itself is in scope and valid (not been released). You are storing this date value in a variable, but not retaining it yourself, so when the date picker goes out of scope you lose the value. As a quick fix, do this instead;
fromDate = [[datePicker date] retain];
Now, this is not the best approach, you really should be making the date a property of whatever class is using this information.
Try put the fromDate variable under class scope, e.g:
#implementation ViewController
{
NSDate *fromDate;
}

Resources