0

I'm working on an iOS project, retrieving data from SQLite through JSON. The table has a datetime column called 'date'. When I retrieve the date in Xcode and printout the result, I see "2012-09-02T16:30:00Z". But when I try to convert it using NSDateFormatter, the result is null.

NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];

NSString *dateString = [dateFormatter stringFromDate:categoryAtIndex.date];
NSLog(@"%@ => %@", categoryAtIndex.date, dateString);

The above outputs:

2012-09-02T16:30:00Z => (null)

in stead of a nicely formatted datestring.

Any thoughts?

Niels
  • 69
  • 6
  • I don't have an answer, but have some things to check: 1) did formatString actually get allocated? 2) Did dateFormatter get allocated? 3) Did dateString get allocated? If they all did, the next thing I would try is printing out the format string like in the example [here](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/cl/NSDateFormatter). Also, try replacing the locale with `[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]` and see if you get anything printed. – user1118321 Sep 02 '12 at 14:15
  • I doubt `categoryAtIndex.date` is `NSDate` since xcode outputs in format like `2012-09-02T16:30:00Z`. It's supposed to look like `2012-09-02 15:06:00 +0000`. Make sure it is `NSDate` object – mask8 Sep 02 '12 at 15:12
  • Thanks for your feedback! I will doublecheck the object, as I might have casted it wrong, using: `self.date = (NSDate *)[theQuestion valueForKey:@"date"];` – Niels Sep 02 '12 at 19:11

1 Answers1

0

Thanks to mask8 I found the issue was related to another part of my code. I found this post on stackoverflow to solve the way to change the format of the date that I retrieved through JSON. I also found another post on stackoverflow describing how to handle conversions from a string to NSDate and vice versa.

Community
  • 1
  • 1
Niels
  • 69
  • 6