arunit21
arunit21

Reputation: 669

NSDateFormatter Locale

How to set Locale for NSDateFormatter? I've tried the below code and its not working.

- (NSString *)localizedStringWithFormat:(NSString *)format {

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:format];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: @"fr_FR"];
    cal.locale = locale;
    df.calendar = cal;
    return [df stringFromDate:self];
}

Please let me know how to make this work.

Thanks.

Upvotes: 7

Views: 15825

Answers (4)

Tung Fam
Tung Fam

Reputation: 8147

in case someone will look for Swift 3 solution:

let dateFormatter = DateFormatter()
let frLocale = Locale(identifier: "fr")
dateFormatter.locale = frLocale

Upvotes: 5

edhnb
edhnb

Reputation: 2192

Adding this as I spent some time on trying to NOT use the localized locale in my date formatter.

From apple docs: Use the system locale when you don’t want any localizations. Use the current locale to format text that you display to users.

Code: [formatter setLocale:[NSLocale systemLocale]];

Upvotes: 2

BastiBen
BastiBen

Reputation: 19870

A bit late to the party, but this is what I did in Swift today:

let df = NSDateFormatter()
df.locale = NSLocale.currentLocale()
df.timeStyle = .MediumStyle
df.dateStyle = .MediumStyle
println("Date: \(df.stringFromDate(obj.dateSent!))")

Depending on your OS region settings, the output should look like this (I'm in Germany):

Date: 27.11.2014 12:30:39

Note: Just figured a lot more people stumble across this SO question, so I guess it can't hurt to answer.

Upvotes: 3

Niralp
Niralp

Reputation: 235

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] 
                     initWithLocaleIdentifier:@"he"];
[dateFormatter setLocale:locale];

Upvotes: 16

Related Questions