سؤال

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"];
هل كانت مفيدة؟

المحلول

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"];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top