views:

2196

answers:

1

I'm using NSDate to get a string such as "18 Jun 09", with code:

NSDate *theDate = [NSDate date];
NSString *dateString = [theDate descriptionWithCalendarFormat:@"%d %b %y"
        timeZone:nil
        locale: nil];

This works, but only results in an English output. I need the output to be localized in the user's default language.

Does Cocoa (Mac OS X 10.4, 10.5 in this case) provide a facility for this localization or do I have to manually localize for each case of day and & month names my self?

(I have provided a locale, but although that does provide a locale-specific ordering of the date, it does not appear to do any localization of day-month names.)

+2  A: 

Here is a snippet from Apple:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];

NSString *formattedDateString = [dateFormatter stringFromDate:date];

NSLog(@"formattedDateString for locale %@: %@", [[dateFormatter locale] localeIdentifier], formattedDateString);
Demi
Thanks for the help. Unfortunately, the snippet does not appear to work correctly. If I change 'date' to [NSDate date], then regardless of the NSDateFormatterXXXStyle used, I get the following result: en_AU: 06/19/09In International System Preferences -> Formats, I have my region set to Australian. BUT! My date preference is really dd/mm/yy. Whereas the code is outputting in the Australian-unfriendly mm/dd/yy format.
SirRatty
Argh. No line breaks in comments?
SirRatty
According to the Apple documentation, NSDateFormatterMediumStyle SHOULD be outputting something like "Jun 19, 2009" instead of 06/19/09.
SirRatty