I've already tried the suggestions in other posts similar to this with no avail. Here is the situation:
I have an NSString like this:
Fri November 18, 2011
and I am trying to convert that to a date like this:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EE MMMM d, YYYY"];
NSDate *myDate = [df dateFromString: dateString];
But the resulting myDate variable holds this:
2010-12-31 00:00:00 +0000
Does anyone have any idea why the wrong date is being picked out from the dateFromString? The string is November 18th, the converted date is December 31st...
Thanks everyone!
You need to use
yyyy
for the year, notYYYY
.From the Apple reference:
And to be correct you also have to change
EE
toEEE
. Though they yield the same result in my test.I think you need "EEE MMMM d, YYYY" i.e. triple E as format string. If this does not work, I recommend using hard coded strings with easier dates and formats to break down the problem. See for example NSDateFormatter not working for more hints on that topic.