Question

I try to make my NSDateFormatter set its locale to German, but it fails. In the code below, while the dateFrom is successfully parsed, dateTo is not.

Note: In German it is Februar, not February.

NSLocale *germanLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"dd. MMMM" options:0
                                                          locale:germanLocale];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:formatString];
NSDate *dateFrom = [dateFormat dateFromString:@"20. September"];
NSDate *dateTo = [dateFormat dateFromString:@"20. Februar"];
Was it helpful?

Solution

Try setting locale of NSDateFormatter:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = germanLocale;
[dateFormat setDateFormat:@"dd. MMMM"];
NSDate *dateFrom = [dateFormat dateFromString:@"20. September"];
NSDate *dateTo = [dateFormat dateFromString:@"20. Februar"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top