我怎么能从月份号码获得月份名称?

18 iphone nsdate

可能重复:
iOS:如何从数字中获取正确的月份名称?

如何从月份编号获取月份名称?

我有像02这样的月份编号,但是希望月份为字符串,在这种情况下为2月.

ing*_*.am 49

像这样的东西:

int monthNumber = 11;
NSString * dateString = [NSString stringWithFormat: @"%d", monthNumber];

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[formatter release];

NSLog(@"%@", stringFromDate);
Run Code Online (Sandbox Code Playgroud)

  • 应该是[格式化程序发布]; 而不是第二个[dateFormatter发布]; (2认同)