I'm having some problems getting an NSString to convert to an NSDate.
Here is my code:
NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);
// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
[self setPublicationDate:[dateFormatter dateFromString:[d objectForKey:@"pubDate"]]];
NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);
When this is run, it logs these messages (it is run several times, I've included just two sets of its log messages):
2012-08-14 12:47:28.446 (App name removed)[9009:707] Unformatted date: Mon, 02 Apr 2012
2012-08-14 12:47:28.454 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.460 (App name removed)[9009:707] Date object description: (null)
2012-08-14 12:47:28.467 (App name removed)[9009:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 12:47:28.474 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.480 (App name removed)[9009:707] Date object description: (null)
I'm testing this code on an iPad touch running iOS 5.1.1
I am able to change the format of the date being received, it is currently being sent from a PHP date function with "D, d M Y"
being used as the format string.
Can anyone please see where I am going wrong? Thank you for your help.
UPDATE 1: I've switched over and ran it on the simulator instead of on a device and for some reason it's worked. Any suggestions as to why it isn't working on the device? Simulator is running iOS 5.1 and the device is running iOS 5.1.1
UPDATE 2: Some people have requested more information, publicationDate
is a synthesised property.
In the .h file:
@property (nonatomic, strong) NSDate *publicationDate;
In the .m file:
@synthesize publicationDate;
I've broken the code up even further:
NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);
NSLog(@"Unformatted date class type: %@", [[d objectForKey:@"pubDate"] class]);
// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
NSString *dateString = [d objectForKey:@"pubDate"];
NSLog(@"dateString: %@", dateString);
NSDate *formattedDate = [dateFormatter dateFromString:dateString];
NSLog(@"formattedDate %@", formattedDate);
[self setPublicationDate:formattedDate];
NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);
and it now logs:
2012-08-14 13:45:35.335 (app name removed)[9167:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 13:45:35.342 (app name removed)[9167:707] Unformatted date class type: __NSCFString
2012-08-14 13:45:35.347 (app name removed)[9167:707] dateString: Sun, 25 Mar 2012
2012-08-14 13:45:35.353 (app name removed)[9167:707] formattedDate (null)
2012-08-14 13:45:35.359 (app name removed)[9167:707] Date object: (null)
2012-08-14 13:45:35.365 (app name removed)[9167:707] Date object description: (null)
UPDATE 3: Success! By changing the date formatter like so:
// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
It now works. I didn't think to mention that the device being tested on is set to Dutch settings (as this is a Dutch development company). Apologies, it never occurred to me that this might effect how the code here worked.