Alex Rivera | Logout

NSDateformatter setDateFormat according to currentLocale

Asked 2011-05-09T15:04:16.810
12

I'm going mad with, probably, a stupid problem.

I have 3 strings: year, month and day. I need to have a date in the right format based on currentLocale, so i.e. if currentLocale localeIdentifier is en_US my dateFormat should be: MMM/dd/yyyy

if it's fr_FR the dateFormat should be dd/MMM/yyyy

I don't think the only way to do this is to get currentLocale localeIdentifier and start with a bunch of if then.

Thanks in advance.

Max

Edit
Report

1 Answer

34

If I understand your question, you want to set your NSDateFormatter to the locale of the user's device. For that you can just do something like this:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
answered 2011-05-09T15:11:35.127

Your Answer