Take the 2-minute tour ×

I have a core data database that is filled up with data coming from a json webservice. But I have a problem with the date. This date is coming back like a string. So first thing I want to do is to transform this string to a NSDate. The second thing is to transform this NSDate in the right format so that I can add a sort descriptor on date.

This is what I have at the moment for how I store my date in the core data database.

 NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MM/dd/yyyy"];
    NSDate* d = [df dateFromString:[genkInfo objectForKey:CALENDAR_DATE]];

    kalender.date              = d;

And how I get my data.

- (void)getKalender // attaches an NSFetchRequest to this UITableViewController
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Kalender"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request


Any idea how to do this?

Kind regards and thank in advance


When I do the following log. I get for each record the correct date and a null value

  NSLog(@"date  = %@",kalender.date);

    2012-10-08 19:17:12.125 RacingGenk[596:c07] date  = 2013-09-02 22:00:00 +0000
    2012-10-08 19:17:12.125 RacingGenk[596:c07] date  = (null)
share|improve this question
It is not clear - are you presenting what you have already tried and found not to be working? If so, in what way? On initial inspection, your code looks fine. Your NSSortDescriptor should work with the NSDate object as-is. –  NSBum Oct 8 '12 at 17:11
I've edited my code. and it is still not sorting –  Stef Geelen Oct 8 '12 at 17:17
"When I log kalender.date I get for each record the correct date and a null value" - What does that mean exactly? kalender has only one date property. Perhaps you can add the NSLog() commands to your code to make clear what is being logged. –  Martin R Oct 8 '12 at 17:31
I've edited my code again. Hope this helps. –  Stef Geelen Oct 8 '12 at 17:37
So the output is from 2 different objects? One has a valid date and the other not? How is the string from the web service formatted? You should check that [df dateFromString:...] does not return nil. –  Martin R Oct 8 '12 at 17:40
show 3 more comments

1 Answer

up vote 1 down vote accepted

As it turned out in the discussion, the format used by the date formatter did not match for date format sent by the web service. Using

[dateFormatter setDateFormat:@"dd/MM/yyyy"]

fixed the problem.

share|improve this answer
add comment

Your Answer


