NSDateFormatter无法解析德语日期

时间:2013-05-02 14:30:47

标签: ios parsing nsdateformatter nslocale

我尝试让我的NSDateFormatter将其语言环境设置为德语,但它失败了。在下面的代码中,dateFrom成功解析后,dateTo不是。

  

注意:德语是Februar,而不是二月。

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"];

1 个答案:

答案 0 :(得分:2)

尝试设置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"];