Karuppu MGR Karuppu MGR - 7 months ago 24
Objective-C Question

Formatting a month string is not working

I'm trying to create month strings that look like "Jan", "Feb", "Mar"... Here is my code:

- (NSString *)getMonthNameString:(int)monthNumber {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"MMM"];

NSArray *monthNames = [formatter standaloneMonthSymbols];
NSString *monthName;

if (monthNumber > 0) {
return monthNames[monthNumber - 1];

return monthNames[1];

So if the month number is 1, I'm expecting the code to provide month name as "Jan" and if it is 2, it has to provide month name as "Feb" and so on. But the problem is that even though I have set the format as
, it is still creating month names of type "January", "February" etc instead of "Jan","Feb" etc. How do I sort this out?



    NSDateFormatter *formate = [NSDateFormatter new];
    [formate setDateFormat:@"MMM"];
    NSArray *monthNames = [formate shortMonthSymbols];
    NSString *monthName;
    if (monthNumber > 0)
        monthName = [monthNames objectAtIndex:(monthNumber - 1)];
        return monthName;

    return [monthNames objectAtIndex:1];