I set the locale and timezone, but when I format the date, I always get an invalid date. The month is always December, and the year is less than the specified year. In my case, I do not need the component of the day.
I checked another similar message, but this did not solve the problem.
Any help is appreciated. Thanks.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setDateFormat:@"MMM YYYY"]; NSDate *formattedDate = [dateFormatter dateFromString:@"Sep 2013"]; NSLog(@"Date: %@", [dateFormatter stringFromDate:formattedDate]); [dateFormatter release]; dateFormatter = nil;
OUTPUT: Date: December 2012
ios nsdateformatter
Nibin v
source share