开发者

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

开发者 https://www.devze.com 2023-01-31 05:06 出处:网络
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.

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, th开发者_开发技巧ere 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.


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.


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


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


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


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)


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消