18

Possible Duplicate:
iOS: How to get a proper Month name from a number?

How can I get month name from month number?

I have month number like 02, but would like the month as a string, in this case February.

Community
  • 1
  • 1
  • 2
    duplicate: http://stackoverflow.com/questions/4488373/ios-how-to-get-a-proper-month-name-from-a-number – phi Feb 03 '11 at 15:00

2 Answers2

49

Something like this:

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);
ingh.am
  • 25,981
  • 43
  • 130
  • 177
3

You can use the monthSymbols array in NSDateFormatter, and then access that array by the key of the month number

mginn
  • 16,036
  • 4
  • 26
  • 54
Mikhail
  • 8,692
  • 8
  • 56
  • 82