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

2019-01-08 08:02发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

Swift 2.0

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

Swift 4.0

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


回答4:

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


回答5:

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


回答6:

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)



回答7:

In Swift 3.0

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

    // result
   "March\n"        


回答8:

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


回答9:

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



回答10:

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


回答11:

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.