IOS: 如何从一个数字中得到一个合适的月份名称?

我知道 NSDateformatter 功能套件对人类来说是一个福音,但同时它也让我感到非常困惑。我希望你能帮我。

在我的代码中,有一个 int 表示一个月,所以: 1表示一月,2表示二月,等等。

在我的用户界面中,我希望将这个整数显示为正确的月份名称。此外,它应该坚持设备的现场。

谢谢你的见解

与此同时,我做了以下事情:

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

是这样做的吗? 似乎有点冗长。

80043 次浏览

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.

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.

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

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)

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

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

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);
let monthName = DateFormatter().monthSymbols[monthNumber - 1]

In Swift 3.0

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


// result
"March\n"

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