All,
I have an NSDateFormatter
, and it doesn't seem to be returning the proper month. Below is my code:
- (NSDate *)stringToDate
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"YYYY-MM-DD"];
return [df dateFromString:self];
}
+ (NSString *)dateToString:(NSDate *)date
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM DD, YYYY"];
return [df stringFromDate:date];
}
+ (NSString *)formatLongDate:(NSString *)string
{
NSDate *date = [string stringToDate];
return [NSString dateToString:date];
}
When I pass in [NSString formatLongDate:@"2013-10-09"]
, the result is January 9, 2013 instead of October 9, 2013. I'm lost as to why this might be happening. Does anyone have an idea?
I think the problem is that DD
is "day of year" rather than "day of month" - and YYYY
is a "week year" rather than just "year". (Week years are odd, basically. Best to just ignore them if possible :)
So try formats of "yyyy-MM-dd" and "MMMM dd, yyyy" instead. It's important to be absolutely precise in the format strings, including casing. When in doubt, check! Follow the links on the Data Formatting Guide page (the exact Unicode Technical Standard version used depends on the version of iOS you're using) for details.
See more on this question at Stackoverflow