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

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

Swift 2.0

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

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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)

Questions:
Answers:

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

Questions:
Answers:

In Swift 3.0

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

    // result
   "March\n"        

Questions:
Answers:

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

Questions:
Answers:

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.