I know that the NSDateformatter suite of functionality is a boon for mankind, but at the same time it is very confusing to me. I hope you can help me out.
Somewhere
You can change the dateFormat of the NSDateFormatter. So to simplify your code:
int monthNumber = 11
NSString * dateString = [NSString stringWithFormat: @"%d", monthNumber];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[formatter setDateFormat:@"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[dateFormatter release];
You should also set the locale once you init the date formatter.
dateFormatter.locale = [NSLocale currentLocale]; // Or any other locale
Hope this helps
How about:
NSUInteger i = <your month integer>;
NSDateFormatter *df = [NSDateFormatter new];
// change locale if the standard is not what you want
NSArray *monthNames = [df standaloneMonthSymbols];
NSString *monthName = [monthNames objectAtIndex:(i - 1)];
[df release];
NSDate to NSString -> As Dateformat Ex: 2015/06/24
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat: @"yyyy/MM/dd"];
NSString *date = [dateformate stringFromDate:selectedDate]; // Convert date to string
NSDate to NSString -> As Dateformat Ex: 2015 June 24, 1:02 PM
[dateformate setDateFormat:@"yyyy MMMM dd, h:mm a"];
NSString *displayDate = [dateformate stringFromDate:selectedDate]; // Convert date to string
NSLog(@"date :%@",date);
NSLog(@"Display time = %@", displayDate);
You should be able to get rid of the release and re-allocation of the dateFormatter, cutting out a couple of lines, but that's all I see.
Swift 4.X
print((DateFormatter().monthSymbols[month-1].capitalized)) //month is int less than 12
For Example:
print((DateFormatter().monthSymbols[11-1].capitalized))
Output
November
Best solution for this is , standaloneMonthSymbols
method,
-(NSString*)MonthNameString:(int)monthNumber
{
NSDateFormatter *formate = [NSDateFormatter new];
NSArray *monthNames = [formate standaloneMonthSymbols];
NSString *monthName = [monthNames objectAtIndex:(monthNumber - 1)];
return monthName;
}