我查看了iOS: How to get a proper Month name from a number?和formatting options,但结果仍然不正确
我的代码看起来像
+ (NSString *)getMonthName:(NSInteger)monthNumber {
if (monthNumber == 0) {
return @"";
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"MMM"];
NSString *monthName = [formatter monthSymbols][(NSUInteger) (monthNumber - 1)];
NSLog(@"monthNumber-%d, monthName-%@", monthNumber, monthName);
return monthName;
}
并将其调用为
self.monthHeaderLabel.text = [NSString stringWithFormat:@"%@", [Helper getMonthName:self.transactionsModel.month]];
当我运行我的应用程序时,我将日志视为
2015-01-10 13:02:17.577 myapp-ios[19175:60b] monthNumber-1, monthName-January
为什么它仍然不正确?我做错了什么?
由于
答案 0 :(得分:0)
根据@ rmaddy的评论,这就是我必须要做的一切
NSString *monthName = [formatter shortMonthSymbols][(NSUInteger) (monthNumber - 1)];
并且效果很好
2015-01-10 13:23:02.223 myapp-ios[19183:60b] monthNumber-1, monthName-Jan
答案 1 :(得分:0)
您可以使用 shortMonthSymbols 代替 monthSymbols
+ (NSString *)getMonthName:(NSInteger)monthNumber {
if (monthNumber == 0) {
return @"";
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *monthName = [formatter shortMonthSymbols][(NSUInteger) (monthNumber - 1)];
NSLog(@"monthNumber-%d, monthName-%@", monthNumber, monthName);
return monthName;
}