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

IosNumbersNsdateformatter

Ios Problem Overview


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 in my code, there is an int representing a month. So: 1 would be January, 2 February, etc.

In my user interface, I would like to display this integer as proper month name. Moreover, it should adhere to the locale of the device.

Thank you for your insights

In the mean time, I have done the following:

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

is this the way to do it? It seems a bit wordy.

Ios Solutions


Solution 1 - Ios

Another option is to use the monthSymbols method:

int monthNumber = 11;	//November
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];

Note that you'll need to subtract 1 from your 1..12 monthNumber since monthSymbols is zero-based.

Solution 2 - Ios

Swift 2.0

let monthName = NSDateFormatter().monthSymbols[monthNumber - 1]

Swift 4.0

let monthName = DateFormatter().monthSymbols[monthNumber - 1]

Solution 3 - Ios

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

Solution 4 - Ios

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;
}

Solution 5 - Ios

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

Solution 6 - Ios

Both answers from Anna Karenina and Carl doesn't work that well as they won't return month name in nominativ for some cultures. I suggest to use the proposed solution from Pascal, which solves this issue (by replacing monthSymbols with standaloneMonthSymbols)

Solution 7 - Ios

In Swift 3.0

    let monthNumber = 3
    let fmt = DateFormatter()
    fmt.dateFormat = "MM"
    let month = fmt.monthSymbols[monthNumber - 1]
    print(month)

    // result
   "March\n"        

Solution 8 - Ios

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

Solution 9 - Ios

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);

Solution 10 - Ios

And with ARC :

+ (NSString *)monthNameFromDate:(NSDate *)date {
    if (!date) return @"n/a";
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MM"];
    return [[df monthSymbols] objectAtIndex:([[df stringFromDate:date] integerValue] - 1)];
}

Solution 11 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSjakelienView Question on Stackoverflow
Solution 1 - Iosuser467105View Answer on Stackoverflow
Solution 2 - IosquemefulView Answer on Stackoverflow
Solution 3 - IosCarlView Answer on Stackoverflow
Solution 4 - IosNicoView Answer on Stackoverflow
Solution 5 - IosPascalView Answer on Stackoverflow
Solution 6 - IosserbView Answer on Stackoverflow
Solution 7 - IosHeshan SandeepaView Answer on Stackoverflow
Solution 8 - IosAshuView Answer on Stackoverflow
Solution 9 - IosPradeepKNView Answer on Stackoverflow
Solution 10 - IosKevin DelordView Answer on Stackoverflow
Solution 11 - IosMatthew FrederickView Answer on Stackoverflow